# Enter your code here. Read input from STDIN. Print output to STDOUT a,b,c = map(int,raw_input().split()) e,f,g = map(int,raw_input().split()) h,i,j = map(int,raw_input().split()) s1 = [8,1,6,3,5,7,4,9,2] s2 = [6,1,8,7,5,3,2,9,4] s3 = [4,9,2,3,5,7,8,1,6] s4 = [2,9,4,7,5,3,6,1,8] s5 = [4,3,8,9,5,1,2,7,6] s6 = [2,7,6,9,5,1,4,3,8] s7 = [8,3,4,1,5,9,6,7,2] s8 = [6,7,2,1,5,9,8,3,4] s_matrix = [a,b,c,e,f,g,h,i,j] s_target = [s1,s2,s3,s4,s5,s6,s7,s8] ret = 100 for s in s_target: val = sum(map(lambda x,y: abs(x-y),s,s_matrix)) if ret > val: ret = val #ret = abs(f -5) #ret += abs(a + b + c - 15) #ret += abs(e+g - 10) #ret += abs(h + i +j -15) print ret #missing_values = list(set([1,2,3,4,5,6,7,8,9]) -set([a,b,c,e,f,g,h,i,j])) #ret += sum(map(lambda x:abs(x-5) if 10-x in missing_values else 0,missing_values)) #print ret