# Enter your code here. Read input from STDIN. Print output to STDOUT L = [[8,1,6],[3,5,7],[4,9,2]] def distance(L1,L2): s = 0 for i in range(3): for j in range(3): s += abs(L1[i][j]-L2[i][j]) return s def rotation(L): temp = L[:] temp[0][1],temp[1][0] = temp[1][0],temp[0][1] temp[0][2],temp[2][0] = temp[2][0],temp[0][2] temp[1][2],temp[2][1] = temp[2][1],temp[1][2] return reflectionx(temp) def reflectionx(L): temp = L[:] for i in range(3): temp[i][0],temp[i][2] = temp[i][2],temp[i][0] return temp def threeRotation(L): temp = L[:] import copy BigL = [] BigL.append(temp) for _ in range(3): temp = rotation(temp) BigL.append(copy.deepcopy(temp)) return BigL def reflectiony(L): temp = L[:] temp[0],temp[2] = temp[2],temp[0] return temp LL = [] #print threeRotation(L) LL += threeRotation(L) LL += threeRotation(reflectionx(L)) LL += threeRotation(reflectiony(L)) LL += threeRotation(reflectionx(reflectiony(L))) LL += threeRotation(reflectiony(reflectionx(L))) m =1000 tar = [] for i in range(3): temp = raw_input().split() temp =map(int,temp) tar.append(temp) #print LL for l in LL: m = min(m,distance(l,tar)) print m