#!/bin/python3 first_row = [int(x) for x in input().strip().split(' ')] second_row = [int(x) for x in input().strip().split(' ')] third_row = [int(x) for x in input().strip().split(' ')] arr = first_row + second_row + third_row m1 = [2,7,6,9,5,1,4,3,8] m2 = [2,9,4,7,5,3,6,1,8] m3 = [6,1,8,7,5,3,2,9,4] m4 = [6,7,2,1,5,9,8,3,4] m5 = [4,9,2,3,5,7,8,1,6] m6 = [4,3,8,9,5,1,2,7,6] m7 = [8,3,4,1,5,9,6,7,2] m8 = [8,1,6,3,5,7,4,9,2] magic_squares = [m1,m2,m3,m4,m5,m6,m7,m8] def cost(arr, square): cost = 0 for i in range(9): cost += abs(arr[i] - square[i]) return cost smallest_cost = 100 for square in magic_squares: c = cost(arr, square) if c < smallest_cost: smallest_cost = c print(smallest_cost)