# Enter your code here. Read input from STDIN. Print output to STDOUT def pm(arr): for i in arr: print i def rotate(mat): ret=[] for i in range(len(mat)): temp=[mat[len(mat)-1][i],mat[len(mat)-2][i],mat[len(mat)-3][i]] ret.append(temp) return ret def col_swap(mat): ret=[] for i in range(len(mat)): temp=[mat[i][2],mat[i][1],mat[i][0]] ret.append(temp) return ret def row_swap(mat): return [mat[2],mat[1],mat[0]] def calc_cost(source,m): s=0 for i in range(3): for j in range(3): s+=abs(source[i][j]-m[i][j]) return s m=[] cost=0 for i in range(3): m.append([]) m[i]=map(int,raw_input().strip().split(' ')) base=[[2, 7, 6], [9, 5, 1], [4, 3, 8]] arr=[] arr.append(base) r=rotate(base) arr.append(r) temp=col_swap(base) arr.append(temp) arr.append(row_swap(temp)) arr.append(row_swap(base)) arr.append(row_swap(r)) arr.append(col_swap(r)) arr.append(row_swap(col_swap(r))) cost=15*9 for i in arr: cost=min(calc_cost(i,m),cost) print cost