possibleSolutions = {0:[[8,1,6],[3,5,7],[4,9,2]], 1:[[6,1,8],[7,5,3],[2,9,4]], 2:[[4,9,2],[3,5,7],[8,1,6]], 3:[[2,9,4],[7,5,3],[6,1,8]], 4:[[8,3,4],[1,5,9],[6,7,2]], 5:[[4,3,8],[9,5,1],[2,7,6]], 6:[[6,7,2],[1,5,9],[8,3,4]], 7:[[2,7,6],[9,5,1],[4,3,8]]} l1=[int(temp) for temp in input().strip().split(' ')] l2=[int(temp) for temp in input().strip().split(' ')] l3=[int(temp) for temp in input().strip().split(' ')] costsList=[] for solution in possibleSolutions.keys(): cost = 0 matrix = possibleSolutions.get(solution) cost += abs(l1[0]-matrix[0][0]) + abs(l1[1]-matrix[0][1]) + abs(l1[2]-matrix[0][2]) cost += abs(l2[0]-matrix[1][0]) + abs(l2[1]-matrix[1][1]) + abs(l2[2]-matrix[1][2]) cost += abs(l3[0]-matrix[2][0]) + abs(l3[1]-matrix[2][1]) + abs(l3[2]-matrix[2][2]) costsList.append(cost) print(min(costsList))