# Algorithm compare against all 8 magic squares return least cost def calcLeastCost(currSquare): validSolutions = [[8,3,4,1,5,9,6,7,2],[6,7,2,1,5,9,8,3,4],[4,9,2,3,5,7,8,1,6],[2,9,4,7,5,3,6,1,8],[2,7,6,9,5,1,4,3,8],[4,3,8,9,5,1,2,7,6],[6,1,8,7,5,3,2,9,4],[8,1,6,3,5,7,4,9,2]] best = 10000 for solution in validSolutions: cost = 0 for i in range (0,9): cost = cost + abs(solution[i] - currSquare[i]) if cost < best: best = cost return best # Read input row1 = [int(height_temp) for height_temp in input().strip().split(' ')] row2 = [int(height_temp) for height_temp in input().strip().split(' ')] row3 = [int(height_temp) for height_temp in input().strip().split(' ')] currSquare = row1 + row2 + row3 cost = calcLeastCost(currSquare) print(int(cost))