import sys row1 = [int(row_temp) for row_temp in input().strip(). split(' ')] row2 = [int(row_temp) for row_temp in input().strip(). split(' ')] row3 = [int(row_temp) for row_temp in input().strip(). split(' ')] s = [ row1, row2, row3 ] s_all = [ [ [4,9,2], [3,5,7], [8,1,6] ], [ [8,1,6], [3,5,7], [4,9,2] ], [ [8,3,4], [1,5,9], [6,7,2] ], [ [4,3,8], [9,5,1], [2,7,6] ], [ [6,7,2], [1,5,9], [8,3,4] ], [ [6,1,8], [7,5,3], [2,9,4] ], [ [2,9,4], [7,5,3], [6,1,8] ], [ [2,7,6], [9,5,1], [4,3,8] ] ] def calc_cost(matrix, s): sum_cost = 0 for row in range(3): for col in range(3): sum_cost += abs(matrix[row][col] - s[row][col]) return sum_cost min_cost = calc_cost(s_all[0], s) for i in range(1, 8): new_cost = calc_cost(s_all[i], s) if new_cost < min_cost: min_cost = new_cost print(min_cost)