def rotateList(A): ''' num = A.pop(len(A)-1) A.insert(0,num) return A ''' B = [0,0,0,0,0,0,0,0] B[0] = A[3] B[1] = A[0] B[2] = A[1] B[3] = A[5] B[4] = A[2] B[5] = A[6] B[6] = A[7] B[7] = A[4] return B def mirrorHor(A): B = [0,0,0,0,0,0,0,0] B[0] = A[2] B[2] = A[0] B[3] = A[4] B[4] = A[3] B[5] = A[7] B[7] = A[5] B[1] = A[1] B[6] = A[6] return B def printSQ(A): print(A[0],A[1],A[2]) print(A[3],5,A[4]) print(A[5],A[6],A[7]) ## Read input A = [] good = [4,9,2,3,7,8,1,6] for i in range(3): line = input().strip().split() line = [int(line[0]), int(line[1]), int(line[2])] for ii in line: A.append(ii) cost = abs(5-A[4]) A.pop(4) #print(A) #printSQ(A) #print() minimum = 81 for i in range(8): #print(i) #printSQ(good) test = 0 for j in range(8): test += abs(A[j] - good[j] ) if test < minimum: minimum = test good = rotateList(good) good = rotateList(good) if i % 4 == 3: if i - 3 == 0: good = mirrorHor(good) #print() #print(A) print(cost+minimum)