r1 = [int(r_temp) for r_temp in input().strip().split(' ')] r2 = [int(r_temp) for r_temp in input().strip().split(' ')] r3 = [int(r_temp) for r_temp in input().strip().split(' ')] square = [r1,r2,r3] sums = dict() sums["r1"] = sum(square[0]) sums["r2"] = sum(square[1]) sums["r3"] = sum(square[2]) sums["c1"] = square[0][0] + square[1][0] + square[2][0] sums["c2"] = square[0][1] + square[1][1] + square[2][1] sums["c3"] = square[0][2] + square[1][2] + square[2][2] sums["main_diag"] = square[0][0] + square[1][1] + square[2][2] sums["sec_diag"] = square[0][2] + square[1][1] + square[2][0] magic1 = [[4, 9, 2], [3, 5, 7], [8, 1, 6]] magic2 = [[2, 9, 4], [7, 5, 3], [6, 1, 8]] magic3 = [[2, 7, 6], [9, 5, 1], [4, 3, 8]] magic4 = [[6, 7, 2], [1, 5, 9], [8, 3, 4]] magic5 = [[6, 1, 8], [7, 5, 3], [2, 9, 4]] magic6 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]] magic7 = [[8, 3, 4], [1, 5, 9], [6, 7, 2]] magic8 = [[4, 3, 8], [9, 5, 1], [2, 7, 6]] magic_squares = [magic1, magic2, magic3, magic4, magic5, magic6, magic7, magic8] cost_vector = dict() square_num = 1 for magic_square in magic_squares: cost = 0 for row in range(0,3): for column in range(0,3): diff = abs(square[row][column] - magic_square[row][column]) cost += diff cost_vector[str(square_num)] = cost square_num += 1 difference = list() for value in cost_vector.values(): difference.append(value) min_cost = min(difference) print(min_cost)