We define a magic square to be an $n\times n$ matrix of distinct positive integers from $\mbox{1}$ to $n^2$ where the sum of any row, column, or diagonal of length $n$ is always equal to the same number:  the magic constant. 

You will be given a $3\times3$ matrix $\boldsymbol{\mathrm{~S~}}$ of integers in the inclusive range $[1,9]$. We can convert any digit $\boldsymbol{a}$ to any other digit $\boldsymbol{b}$ in the range $[1,9]$ at cost of $\left|a-b\right|$.  Given $\boldsymbol{\mathrm{~S~}}$, convert it into a magic square at minimal cost. Print this cost on a new line.

Note: The resulting magic square must contain distinct integers in the inclusive range $[1,9]$.

Example  

$s = [[5, 3, 4], [1, 5, 8], [6, 4, 2]]$  

The matrix looks like this: 

5 3 4
1 5 8
6 4 2

We can convert it to the following magic square:

8 3 4
1 5 9
6 7 2

This took three replacements at a cost of $|5-8|+|8-9|+|4-7|=7$.

Function Description

Complete the formingMagicSquare function in the editor below.  

formingMagicSquare has the following parameter(s):  

int s[3][3]: a $3\times3$ array of integers  

Returns  

int:  the minimal total cost of converting the input square to a magic square 

Input Format

Each of the $3$ lines contains three space-separated integers of row $s[i]$.  

Constraints

$s[i][j]\in[1,9]$

Sample Input 0
4 9 2
3 5 7
8 1 5

Sample Output 0
1

Explanation 0

If we change the bottom right value, $s[2][2]$, from $5$ to $\boldsymbol{6}$ at a cost of $|6-5|=1$, $\boldsymbol{\mathrm{~S~}}$ becomes a magic square at the minimum possible cost.

Sample Input 1
4 8 2
4 5 7
6 1 6

Sample Output 1
4

Explanation 1

Using 0-based indexing, if we make 

$s[0][1]$->$\mbox{9}$ at a cost of $|9-8|=1$ 
$s[\mathbf{1}][\mathbf{0}]$->$3$ at a cost of $|3-4|=1$
$s[2][0]$->$8$ at a cost of $|8-6|=2$,  

then the total cost will be $1+1+2=4$.

Difficulty: MEDIUM
Tags: Implementation

Starter code:

Solutions:
from itertools import *
X = []
X.extend(list(map(int, input().split())))
X.extend(list(map(int, input().split())))
X.extend(list(map(int, input().split())))
Ans = 81
for P in permutations(range(1, 10)):
	if sum(P[0:3]) == 15 and sum(P[3:6]) == 15 and (sum(P[0::3]) == 15) and (sum(P[1::3]) == 15) and (P[0] + P[4] + P[8] == 15) and (P[2] + P[4] + P[6] == 15):
		Ans = min(Ans, sum((abs(P[i] - X[i]) for i in range(0, 9))))
print(Ans)


import itertools
s = []
for i in range(3):
	s.extend(list(map(int, input().split(' '))))
min_cost = 1000
best = None

def is_magic(s):
	for i in range(3):
		if sum(s[i * 3:i * 3 + 3]) != 15:
			return False
		if sum(s[i::3]) != 15:
			return False
	if s[0] + s[4] + s[8] != 15:
		return False
	if s[2] + s[4] + s[6] != 15:
		return False
	return True
best = None
for p in itertools.permutations(range(1, 10)):
	cost = sum([abs(p[i] - s[i]) for i in range(len(s))])
	if cost < min_cost and is_magic(p):
		min_cost = cost
		best = p
print(min_cost)


input_nums = []
input_nums.extend([int(i) for i in input().split()])
input_nums.extend([int(i) for i in input().split()])
input_nums.extend([int(i) for i in input().split()])
pos = []
pos.append([8, 1, 6, 3, 5, 7, 4, 9, 2])
pos.append([6, 1, 8, 7, 5, 3, 2, 9, 4])
pos.append([4, 9, 2, 3, 5, 7, 8, 1, 6])
pos.append([2, 9, 4, 7, 5, 3, 6, 1, 8])
pos.append([8, 3, 4, 1, 5, 9, 6, 7, 2])
pos.append([4, 3, 8, 9, 5, 1, 2, 7, 6])
pos.append([6, 7, 2, 1, 5, 9, 8, 3, 4])
pos.append([2, 7, 6, 9, 5, 1, 4, 3, 8])
mindiff = 10000
for arr in pos:
	diff = 0
	for i in range(9):
		diff += abs(arr[i] - input_nums[i])
	if diff < mindiff:
		mindiff = diff
print(mindiff)


possibilities = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
s = []
for i in range(3):
	s.append([int(x) for x in input().split(' ')])
min_cost = None
for p in possibilities:
	cost = 0
	for i in range(3):
		for j in range(3):
			cost += abs(p[i][j] - s[i][j])
	if min_cost is None or cost < min_cost:
		min_cost = cost
print(min_cost)


magic_squares = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = [[], [], []]
for i in range(3):
	matrix[i] = [int(a) for a in input().split(' ')]
min_dist = None
for square in magic_squares:
	dist = 0
	for i in range(len(square)):
		for j in range(len(square[i])):
			dist += abs(square[i][j] - matrix[i][j])
	if min_dist is None or dist < min_dist:
		min_dist = dist
print(min_dist)


squares = [[[2, 7, 6], [9, 5, 1], [4, 3, 8]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]]]
m = []
for i in range(3):
	mtmp = [int(k) for k in input().strip().split(' ')]
	m.append(mtmp)
mindistance = 1000
for i in range(len(squares)):
	tsq = squares[i]
	distance = 0
	for j in range(3):
		for k in range(3):
			distance += abs(tsq[j][k] - m[j][k])
	if distance < mindistance:
		mindistance = distance
print(str(mindistance))


import sys
from math import fabs
matrix = [[int(x) for x in input().strip().split()], [int(x) for x in input().strip().split()], [int(x) for x in input().strip().split()]]
allmagic = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[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 edit_distance(matrix1, matrix2):
	dist = 0
	for i in range(len(matrix1)):
		for j in range(len(matrix1[i])):
			dist += abs(matrix1[i][j] - matrix2[i][j])
	return dist
dist = min((edit_distance(matrix, magic) for magic in allmagic))
print(dist)


def rotatelist(mylist):
	return [[mylist[j][2 - i] for j in range(3)] for i in range(3)]

def changecost(square1, square2):
	return sum([abs(square1[r][c] - square2[r][c]) for r in range(3) for c in range(3)])

def lucassquare(a, b, c):
	x = [[], [], []]
	x[0] = [c - b, c + a + b, c - a]
	x[1] = [c - (a - b), c, c + (a - b)]
	x[2] = [c + a, c - (a + b), c + b]
	return x
square = [[], [], []]
for r in range(3):
	row = list(map(int, input().strip().split(' ')))
	square[r] = row
magicref = [[2, 9, 4], [7, 5, 3], [6, 1, 8]]
magicflip = magicref[:][::-1]
sq1 = magicref
sq2 = magicflip
for i in range(4):
	if i == 0:
		mincost = min(changecost(sq1, square), changecost(sq2, square))
	else:
		mincost = min(changecost(sq1, square), changecost(sq2, square), mincost)
	sq1 = rotatelist(sq1)
	sq2 = rotatelist(sq2)
print(mincost)


rowlist = []
matrix = []
odd = []
for i in range(3):
	row = input()
	rowlist = row.split(' ')
	matrix.append(rowlist)
matrix1 = [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
matrix2 = [[2, 9, 4], [7, 5, 3], [6, 1, 8]]
matrix3 = [[4, 3, 8], [9, 5, 1], [2, 7, 6]]
matrix4 = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
matrix5 = [[6, 7, 2], [1, 5, 9], [8, 3, 4]]
matrix6 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
matrix7 = [[8, 3, 4], [1, 5, 9], [6, 7, 2]]
matrix8 = [[6, 1, 8], [7, 5, 3], [2, 9, 4]]
cost1 = 0
for i in range(3):
	for j in range(3):
		cost1 += abs(int(matrix[i][j]) - matrix1[i][j])
cost2 = 0
for i in range(3):
	for j in range(3):
		cost2 += abs(int(matrix[i][j]) - matrix2[i][j])
cost3 = 0
for i in range(3):
	for j in range(3):
		cost3 += abs(int(matrix[i][j]) - matrix3[i][j])
cost4 = 0
for i in range(3):
	for j in range(3):
		cost4 += abs(int(matrix[i][j]) - matrix4[i][j])
cost5 = 0
for i in range(3):
	for j in range(3):
		cost5 += abs(int(matrix[i][j]) - matrix5[i][j])
cost6 = 0
for i in range(3):
	for j in range(3):
		cost6 += abs(int(matrix[i][j]) - matrix6[i][j])
cost7 = 0
for i in range(3):
	for j in range(3):
		cost7 += abs(int(matrix[i][j]) - matrix7[i][j])
cost8 = 0
for i in range(3):
	for j in range(3):
		cost8 += abs(int(matrix[i][j]) - matrix8[i][j])
print(min(cost1, cost2, cost3, cost4, cost5, cost6, cost7, cost8))


def getCostElement(a, b):
	cost = a - b
	if cost < 0:
		cost = -cost
	return cost
magicSquares = []
magicSquares.append([8, 1, 6, 3, 5, 7, 4, 9, 2])
magicSquares.append([6, 7, 2, 1, 5, 9, 8, 3, 4])
magicSquares.append([4, 3, 8, 9, 5, 1, 2, 7, 6])
magicSquares.append([2, 9, 4, 7, 5, 3, 6, 1, 8])
magicSquares.append([8, 3, 4, 1, 5, 9, 6, 7, 2])
magicSquares.append([6, 1, 8, 7, 5, 3, 2, 9, 4])
magicSquares.append([4, 9, 2, 3, 5, 7, 8, 1, 6])
magicSquares.append([2, 7, 6, 9, 5, 1, 4, 3, 8])
matrix = list(map(int, input().split() + input().split() + input().split()))
answers = []
for n in range(8):
	tempCost = 0
	for (i, j) in zip(matrix, magicSquares[n]):
		tempCost += getCostElement(i, j)
	answers.append(tempCost)
print(min(answers))


from itertools import permutations

def linesum(m, line):
	return sum((m[y][x] for (y, x) in line))

def complete(m):
	return len(set((linesum(m, i) for i in alld))) == 1

def all_combs():
	for i in permutations(range(1, 10)):
		i = [i[:3], i[3:6], i[6:]]
		if complete(i):
			yield i

def difference(m, a):
	return sum((sum((abs(m[ii][i] - a[ii][i]) for i in range(3))) for ii in range(3)))
rows = [[(y, x) for x in range(3)] for y in range(3)]
cols = [[(y, x) for y in range(3)] for x in range(3)]
diagonals = [[(0, 0), (1, 1), (2, 2)], [(0, 2), (1, 1), (2, 0)]]
alld = rows + cols + diagonals
m = [[int(i) for i in input().split()] for i in range(3)]
print(min((difference(m, i) for i in all_combs())))


arr = []
for j in range(3):
	temp = [int(x) for x in input().split()]
	arr.append(temp)

def compare(arr, arr2):
	c = 0
	for i in range(3):
		for j in range(3):
			if not arr[i][j] == arr2[i][j]:
				c = c + abs(arr[i][j] - arr2[i][j])
	return c
m = float('inf')
x = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]]]
for j in range(8):
	m = min(m, compare(arr, x[j]))
	arr2 = arr
print(m)


S = []
for _ in range(3):
	r = list(map(int, input().split()))
	S.append(r)
A = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
B = [[2, 9, 4], [7, 5, 3], [6, 1, 8]]
C = [[8, 3, 4], [1, 5, 9], [6, 7, 2]]
D = [[4, 3, 8], [9, 5, 1], [2, 7, 6]]
E = [[6, 7, 2], [1, 5, 9], [8, 3, 4]]
F = [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
G = [[6, 1, 8], [7, 5, 3], [2, 9, 4]]
H = [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
L = [A, B, C, D, E, F, G, H]
cost = 72
for ms in L:
	diff = 0
	for i in range(3):
		for j in range(3):
			diff += abs(S[i][j] - ms[i][j])
	if diff < cost:
		cost = diff
print(cost)


sqrs = [[8, 1, 6, 3, 5, 7, 4, 9, 2], [6, 1, 8, 7, 5, 3, 2, 9, 4], [4, 9, 2, 3, 5, 7, 8, 1, 6], [2, 9, 4, 7, 5, 3, 6, 1, 8], [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], [2, 7, 6, 9, 5, 1, 4, 3, 8]]
inp = [int(i) for i in input().split() + input().split() + input().split()]
m = -1
for s in sqrs:
	r = sum([abs(i - j) for (i, j) in zip(inp, s)])
	if r < m or m == -1:
		m = r
print(m)


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)


matrix = [list(map(int, input().split(' '))), list(map(int, input().split(' '))), list(map(int, input().split(' ')))]
lowest = 100
m1 = [[2, 9, 4], [7, 5, 3], [6, 1, 8]]
m2 = [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
m3 = [[6, 7, 2], [1, 5, 9], [8, 3, 4]]
m4 = [[6, 1, 8], [7, 5, 3], [2, 9, 4]]
m5 = [[4, 3, 8], [9, 5, 1], [2, 7, 6]]
m6 = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
m7 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
m8 = [[8, 3, 4], [1, 5, 9], [6, 7, 2]]
matrices = [m1, m2, m3, m4, m5, m6, m7, m8]
for i in range(0, 8):
	temp = matrices[i]
	difference = 0
	for y in range(0, 3):
		for x in range(0, 3):
			difference += abs(matrix[y][x] - temp[y][x])
	if difference < lowest:
		lowest = difference
	if difference == 0:
		break
print(lowest)


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
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))


s = []
for i in range(3):
	s.append([int(i) for i in input().split()])
orig = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
all_squares = [orig]
all_squares.append(orig[::-1])
all_squares.append([i[::-1] for i in orig])
all_squares.append(all_squares[2][::-1])
all_squares.append([[4, 3, 8], [9, 5, 1], [2, 7, 6]])
all_squares.append(all_squares[4][::-1])
all_squares.append([i[::-1] for i in all_squares[4]])
all_squares.append(all_squares[6][::-1])
least = 99
for i in all_squares:
	temp = 0
	for j in range(3):
		for k in range(3):
			temp += abs(s[j][k] - i[j][k])
	if temp < least:
		least = temp
print(least)


def find_diff(s1, s2):
	sum = 0
	for i in range(3):
		for j in range(3):
			sum += abs(s1[i][j] - s2[i][j])
	return sum
ans_sq = [[[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[8, 1, 6], [3, 5, 7], [4, 9, 2]]]
s = []
for i in range(3):
	s.append([int(x) for x in input().split()])
diffs = [find_diff(s, x) for x in ans_sq]
print(min(diffs))


matrix = [[0] * 3] * 3
for i in range(3):
	matrix[i] = [int(j) for j in input().strip().split(' ')]
squares = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
min_cost = 15 * 9
for sq in squares:
	cost = 0
	for i in range(3):
		for j in range(3):
			cost += abs(sq[i][j] - matrix[i][j])
	if cost < min_cost:
		min_cost = cost
print(min_cost)


import sys
line1 = [int(word) for word in input().strip().split(' ')]
line2 = [int(word) for word in input().strip().split(' ')]
line3 = [int(word) for word in input().strip().split(' ')]
line = line1 + line2 + line3
magicmatrixes = [[2, 7, 6, 9, 5, 1, 4, 3, 8], [2, 9, 4, 7, 5, 3, 6, 1, 8], [4, 3, 8, 9, 5, 1, 2, 7, 6], [4, 9, 2, 3, 5, 7, 8, 1, 6]]
cost = 100
for i in magicmatrixes:
	cost = min(cost, sum((abs(line[j] - i[j]) for j in range(9))))
	cost = min(cost, sum((abs(line[j] - i[8 - j]) for j in range(9))))
print(cost)


masterlist = [[8, 1, 6, 3, 5, 7, 4, 9, 2], [4, 3, 8, 9, 5, 1, 2, 7, 6], [2, 9, 4, 7, 5, 3, 6, 1, 8], [6, 7, 2, 1, 5, 9, 8, 3, 4], [6, 1, 8, 7, 5, 3, 2, 9, 4], [8, 3, 4, 1, 5, 9, 6, 7, 2], [4, 9, 2, 3, 5, 7, 8, 1, 6], [2, 7, 6, 9, 5, 1, 4, 3, 8]]

def compareSquares(first, second):
	return sum((abs(first[x] - second[x]) for x in range(9)))
candidate = []
for _ in range(3):
	candidate.extend([int(x) for x in input().strip().split()])
print(min((compareSquares(masterlist[y], candidate) for y in range(8))))


square = []
for i in range(3):
	square.extend([int(square_temp) for square_temp in input().strip().split(' ')])
cnt = 0
a = []
a.append([4, 9, 2, 3, 5, 7, 8, 1, 6])
a.append([2, 9, 4, 7, 5, 3, 6, 1, 8])
a.append([8, 1, 6, 3, 5, 7, 4, 9, 2])
a.append([6, 1, 8, 7, 5, 3, 2, 9, 4])
a.append([4, 3, 8, 9, 5, 1, 2, 7, 6])
a.append([8, 3, 4, 1, 5, 9, 6, 7, 2])
a.append([2, 7, 6, 9, 5, 1, 4, 3, 8])
a.append([6, 7, 2, 1, 5, 9, 8, 3, 4])
b = [0] * 8
for i in range(9):
	for j in range(8):
		b[j] += abs(square[i] - a[j][i])
print(min(b))


def compute_diff(square, etalon):
	res = 0
	for i in range(3):
		for ii in range(3):
			res += abs(square[i][ii] - etalon[i][ii])
	return res
square = [[int(x) for x in input().split()] for i in range(3)]
etalons = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
min_dif = 9 * 10
for etalon in etalons:
	t = compute_diff(square, etalon)
	if t < min_dif:
		min_dif = t
print(min_dif)


def compare(sq1, sq2):
	result = 0
	for i in range(3):
		for j in range(3):
			result += abs(sq1[i][j] - sq2[i][j])
	return result
ms = []
ms.append([[8, 1, 6], [3, 5, 7], [4, 9, 2]])
ms.append([[4, 3, 8], [9, 5, 1], [2, 7, 6]])
ms.append([[2, 9, 4], [7, 5, 3], [6, 1, 8]])
ms.append([[6, 7, 2], [1, 5, 9], [8, 3, 4]])
ms.append([[6, 1, 8], [7, 5, 3], [2, 9, 4]])
ms.append([[8, 3, 4], [1, 5, 9], [6, 7, 2]])
ms.append([[4, 9, 2], [3, 5, 7], [8, 1, 6]])
ms.append([[2, 7, 6], [9, 5, 1], [4, 3, 8]])
sq1 = [[int(i) for i in input().split()] for n in range(3)]
min_cost = 1000000
for sq2 in ms:
	cost = compare(sq1, sq2)
	if cost < min_cost:
		min_cost = cost
	if min_cost == 0:
		break
print(min_cost)


def readints():
	return list(map(int, input().strip().split()))

def transpose(m):
	return list(zip(*m))

def reverseRows(m):
	return [list(reversed(r)) for r in m]

def rotate90(m):
	return reverseRows(transpose(m))
magic = [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
no_reflections = [magic, rotate90(magic), rotate90(rotate90(magic)), rotate90(rotate90(rotate90(magic)))]
all_magic = no_reflections + [reverseRows(m) for m in no_reflections]

def read_matrix():
	return [readints() for _ in range(3)]

def matrix_diff(m1, m2):
	err = 0
	for (row1, row2) in zip(m1, m2):
		for (a, b) in zip(row1, row2):
			err += abs(a - b)
	return err

def main():
	matrix = read_matrix()
	print(min((matrix_diff(magic, matrix) for magic in all_magic)))
main()


def rotateList(A):
	B = [0, 0, 0, 0, 0, 0, 0, 0]
	B[0] = A[3]
	B[1] = A[0]
	B[2] = A[1]
	B[3] = A[5]
	B[4] = A[2]
	B[5] = A[6]
	B[6] = A[7]
	B[7] = A[4]
	return B

def mirrorHor(A):
	B = [0, 0, 0, 0, 0, 0, 0, 0]
	B[0] = A[2]
	B[2] = A[0]
	B[3] = A[4]
	B[4] = A[3]
	B[5] = A[7]
	B[7] = A[5]
	B[1] = A[1]
	B[6] = A[6]
	return B

def printSQ(A):
	print(A[0], A[1], A[2])
	print(A[3], 5, A[4])
	print(A[5], A[6], A[7])
A = []
good = [4, 9, 2, 3, 7, 8, 1, 6]
for i in range(3):
	line = input().strip().split()
	line = [int(line[0]), int(line[1]), int(line[2])]
	for ii in line:
		A.append(ii)
cost = abs(5 - A[4])
A.pop(4)
minimum = 81
for i in range(8):
	test = 0
	for j in range(8):
		test += abs(A[j] - good[j])
	if test < minimum:
		minimum = test
	good = rotateList(good)
	good = rotateList(good)
	if i % 4 == 3:
		if i - 3 == 0:
			good = mirrorHor(good)
print(cost + minimum)


ms1 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
ms2 = [[8, 3, 4], [1, 5, 9], [6, 7, 2]]
ms3 = [[6, 7, 2], [1, 5, 9], [8, 3, 4]]
ms4 = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
ms5 = [[2, 9, 4], [7, 5, 3], [6, 1, 8]]
ms6 = [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
ms7 = [[4, 3, 8], [9, 5, 1], [2, 7, 6]]
ms8 = [[6, 1, 8], [7, 5, 3], [2, 9, 4]]
m1 = list(map(int, input().split()))
m2 = list(map(int, input().split()))
m3 = list(map(int, input().split()))
notMagic = [m1, m2, m3]
costs = []
cost = 0
for row in range(3):
	for col in range(3):
		cost += abs(notMagic[row][col] - ms1[row][col])
costs.append(cost)
cost = 0
for row in range(3):
	for col in range(3):
		cost += abs(notMagic[row][col] - ms2[row][col])
costs.append(cost)
cost = 0
for row in range(3):
	for col in range(3):
		cost += abs(notMagic[row][col] - ms3[row][col])
costs.append(cost)
cost = 0
for row in range(3):
	for col in range(3):
		cost += abs(notMagic[row][col] - ms4[row][col])
costs.append(cost)
cost = 0
for row in range(3):
	for col in range(3):
		cost += abs(notMagic[row][col] - ms5[row][col])
costs.append(cost)
cost = 0
for row in range(3):
	for col in range(3):
		cost += abs(notMagic[row][col] - ms6[row][col])
costs.append(cost)
cost = 0
for row in range(3):
	for col in range(3):
		cost += abs(notMagic[row][col] - ms7[row][col])
costs.append(cost)
cost = 0
for row in range(3):
	for col in range(3):
		cost += abs(notMagic[row][col] - ms8[row][col])
costs.append(cost)
print(min(costs))


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))


row1 = list(map(int, list(input().split(' '))))
row2 = list(map(int, list(input().split(' '))))
row3 = list(map(int, list(input().split(' '))))
squares = [[[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]]]

def cost(magic, input):
	cost = 0
	for i in range(3):
		for j in range(3):
			cost += abs(magic[i][j] - input[i][j])
	return cost

def it(s):
	return [cost(squares[i], s) for i in range(len(squares))]
print(min(it([row1, row2, row3])))


row = list()
for i in range(3):
	temp = list(input().split())
	temp = [int(x) for x in temp]
	row.append(temp)
cost = 0
cost += abs(5 - row[1][1])
a = abs(4 - row[0][0]) + abs(9 - row[0][1]) + abs(2 - row[0][2]) + abs(3 - row[1][0]) + abs(7 - row[1][2]) + abs(8 - row[2][0]) + abs(1 - row[2][1]) + abs(6 - row[2][2])
b = abs(2 - row[0][0]) + abs(7 - row[0][1]) + abs(6 - row[0][2]) + abs(9 - row[1][0]) + abs(1 - row[1][2]) + abs(4 - row[2][0]) + abs(3 - row[2][1]) + abs(8 - row[2][2])
c = abs(6 - row[0][0]) + abs(1 - row[0][1]) + abs(8 - row[0][2]) + abs(7 - row[1][0]) + abs(3 - row[1][2]) + abs(2 - row[2][0]) + abs(9 - row[2][1]) + abs(4 - row[2][2])
d = abs(8 - row[0][0]) + abs(3 - row[0][1]) + abs(4 - row[0][2]) + abs(1 - row[1][0]) + abs(9 - row[1][2]) + abs(6 - row[2][0]) + abs(7 - row[2][1]) + abs(2 - row[2][2])
e = abs(2 - row[0][0]) + abs(9 - row[0][1]) + abs(4 - row[0][2]) + abs(7 - row[1][0]) + abs(3 - row[1][2]) + abs(6 - row[2][0]) + abs(1 - row[2][1]) + abs(8 - row[2][2])
f = abs(4 - row[0][0]) + abs(3 - row[0][1]) + abs(8 - row[0][2]) + abs(9 - row[1][0]) + abs(1 - row[1][2]) + abs(2 - row[2][0]) + abs(7 - row[2][1]) + abs(6 - row[2][2])
g = abs(8 - row[0][0]) + abs(1 - row[0][1]) + abs(6 - row[0][2]) + abs(3 - row[1][0]) + abs(7 - row[1][2]) + abs(4 - row[2][0]) + abs(9 - row[2][1]) + abs(2 - row[2][2])
h = abs(6 - row[0][0]) + abs(7 - row[0][1]) + abs(2 - row[0][2]) + abs(1 - row[1][0]) + abs(9 - row[1][2]) + abs(8 - row[2][0]) + abs(3 - row[2][1]) + abs(4 - row[2][2])
cost += min(a, b, c, d, e, f, g, h)
print(cost)


mss = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]]]

def diff(a, b, transposed=False):
	s = 0
	for i in range(3):
		for j in range(3):
			bi = b[j][i] if transposed else b[i][j]
			s += abs(a[i][j] - bi)
	return s
get_row = lambda : [int(n) for n in input().strip().split()]
m = []
for i in range(3):
	m.append(get_row())
minimum = 100
for ms in mss:
	d = diff(m, ms)
	minimum = min(minimum, d)
for ms in mss:
	d = diff(m, ms, transposed=True)
	minimum = min(minimum, d)
print(minimum)


import sys

def all_equivalent(m):

	def all_reversed(m):
		yield m
		yield reversed(m)

		def hor_reversed(rows):
			return map(reversed, rows)
		yield hor_reversed(m)
		yield hor_reversed(reversed(m))
	for r in all_reversed(m):
		yield r
	for r in all_reversed([list(t) for t in zip(*m)]):
		yield r

def cost(s, m):
	ss = (si for row in s for si in row)
	ms = (mi for row in m for mi in row)
	return sum((abs(si - mi) for (si, mi) in zip(ss, ms)))
magic = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
all_magic = all_equivalent(magic)
s = [list(map(int, line.split())) for line in sys.stdin.readlines()]
print(min((cost(s, m) for m in all_magic)))


s = [int(s) for s in input().strip().split()]
s.extend([int(s) for s in input().strip().split()])
s.extend([int(s) for s in input().strip().split()])
magicsquares = []
costs = []
for a in [2, 4, 6, 8]:
	for b in [2, 4, 6, 8]:
		if a + b != 10 and a != b:
			m = [a, 15 - a - b, b, 15 - a - (10 - b), 5, 15 - b - (10 - a), 10 - b, 10 - (15 - a - b), 10 - a]
			magicsquares.append(m)
			costs.append(sum([abs(x - y) for (x, y) in zip(s, m)]))
print(min(costs))


squares = [[4, 9, 2, 3, 5, 7, 8, 1, 6], [2, 7, 6, 9, 5, 1, 4, 3, 8], [6, 1, 8, 7, 5, 3, 2, 9, 4], [8, 3, 4, 1, 5, 9, 6, 7, 2], [2, 9, 4, 7, 5, 3, 6, 1, 8], [6, 7, 2, 1, 5, 9, 8, 3, 4], [8, 1, 6, 3, 5, 7, 4, 9, 2], [4, 3, 8, 9, 5, 1, 2, 7, 6]]
row1 = list(map(int, input().split()))
row2 = list(map(int, input().split()))
row3 = list(map(int, input().split()))
matrix = row1 + row2 + row3
min_cost = 90
cost = 0
for square in squares:
	cost = 0
	for i in range(9):
		cost += abs(square[i] - matrix[i])
	if cost < min_cost:
		min_cost = cost
print(min_cost)


input_square = [[int(value) for value in input().strip().split()] for i in range(3)]
magic_squares = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]

def cost(initial_square, other_square):
	cost = 0
	for (initial_column, other_column) in zip(initial_square, other_square):
		for (initial_value, other_value) in zip(initial_column, other_column):
			cost += abs(initial_value - other_value)
	return cost

def lowest_cost(square, other_squares):
	return min((cost(square, other) for other in other_squares))
print(lowest_cost(input_square, magic_squares))


import sys

def square_diff(M1, M2, n=3):
	result = 0
	for i in range(n):
		for j in range(n):
			result += abs(M1[i][j] - M2[i][j])
	return result
possible_squares = [[[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
rows = sys.stdin.read().strip().split('\n')
M = list(map(lambda row: [int(d) for d in row.split(' ')], rows))
diffs = []
for candidate in possible_squares:
	diffs.append(square_diff(M, candidate))
print(min(diffs))


def rot_clockwise(m):
	new_m = []
	for i in range(3):
		new_m.append(m[i][:])
	tmp = new_m[0][0]
	new_m[0][0] = new_m[2][0]
	new_m[2][0] = new_m[2][2]
	new_m[2][2] = new_m[0][2]
	new_m[0][2] = tmp
	tmp = new_m[0][1]
	new_m[0][1] = new_m[1][0]
	new_m[1][0] = new_m[2][1]
	new_m[2][1] = new_m[1][2]
	new_m[1][2] = tmp
	return new_m

def reflect_vert(m):
	new_m = []
	for i in range(3):
		new_m.append(m[i][:])
		tmp = new_m[i][0]
		new_m[i][0] = new_m[i][2]
		new_m[i][2] = tmp
	return new_m

def get_cost(m, n):
	cost = 0
	for r in range(3):
		for c in range(3):
			cost += abs(m[r][c] - n[r][c])
	return cost
square = []
for n in range(3):
	square.append(list(map(lambda x: int(x), input().split())))
lo_shu = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
matrices = [lo_shu]
matrices.append(reflect_vert(lo_shu))
for n in range(2, 8, 2):
	rotated = rot_clockwise(matrices[-2])
	reflected = reflect_vert(rotated)
	matrices.append(rotated)
	matrices.append(reflected)
min_cost = get_cost(matrices[0], square)
for n in range(1, 8):
	cost = get_cost(matrices[n], square)
	if cost < min_cost:
		min_cost = cost
print(min_cost)


import itertools

def sum_diag(square, primary=True):
	n = len(square)
	if primary:
		return sum([square[x][x] for x in range(n)])
	else:
		return sum([square[row][n - row - 1] for row in range(n)])

def transpose(square):
	n = len(square)
	return [[square[j][i] for j in range(n)] for i in range(n)]

def is_magic(square):
	n = len(square)
	sumValue = 0
	for (i, row) in enumerate(square):
		if i == 0:
			sumValue = sum(row)
		elif sum(row) != sumValue:
			return False
	for col in transpose(square):
		if sum(col) != sumValue:
			return False
	if sum_diag(square) != sumValue:
		return False
	if sum_diag(square, primary=False) != sumValue:
		return False
	return True

def magic_permutaions():
	perms = itertools.permutations(range(1, 10))
	perms = [list(case) for case in perms]
	perms = [[case[0:3], case[3:6], case[6:9]] for case in perms]
	perms = [case for case in perms if is_magic(case)]
	return perms

def cost(fromMat, toMat):
	n = len(fromMat)
	return sum([abs(fromMat[i][j] - toMat[i][j]) for i in range(n) for j in range(n)])
matrix = []
for i in range(3):
	matrix.append([int(x) for x in input().split(' ')])
minimum = min([cost(matrix, perm) for perm in magic_permutaions()])
print(minimum)


r1 = input().split()
r2 = input().split()
r3 = input().split()
calc = [[int(i) for i in r1], [int(i) for i in r2], [int(i) for i in r3]]
l1 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
l2 = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
l3 = [[8, 3, 4], [1, 5, 9], [6, 7, 2]]
l4 = [[6, 7, 2], [1, 5, 9], [8, 3, 4]]
l5 = [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
l6 = [[2, 9, 4], [7, 5, 3], [6, 1, 8]]
l7 = [[6, 1, 8], [7, 5, 3], [2, 9, 4]]
l8 = [[4, 3, 8], [9, 5, 1], [2, 7, 6]]

def main(l):
	diff = 0
	for i in range(0, 3):
		for j in range(0, 3):
			diff += abs(calc[i][j] - l[i][j])
	return diff
print(min(main(l1), main(l2), main(l3), main(l4), main(l5), main(l6), main(l7), main(l8)))


l1 = input().strip().split(' ')
l2 = input().strip().split(' ')
l3 = input().strip().split(' ')
magic = {}
magic['a'] = int(l1[0])
magic['b'] = int(l1[1])
magic['c'] = int(l1[2])
magic['d'] = int(l2[0])
magic['e'] = int(l2[1])
magic['f'] = int(l2[2])
magic['g'] = int(l3[0])
magic['h'] = int(l3[1])
magic['i'] = int(l3[2])
m1 = [8, 1, 6, 3, 5, 7, 4, 9, 2]
m2 = [8, 3, 4, 1, 5, 9, 6, 7, 2]
m3 = [6, 7, 2, 1, 5, 9, 8, 3, 4]
m4 = [4, 9, 2, 3, 5, 7, 8, 1, 6]
m5 = [2, 9, 4, 7, 5, 3, 6, 1, 8]
m6 = [2, 7, 6, 9, 5, 1, 4, 3, 8]
m7 = [4, 3, 8, 9, 5, 1, 2, 7, 6]
m8 = [6, 1, 8, 7, 5, 3, 2, 9, 4]

def ScoreFunc(myList=[]):
	score = abs(magic['a'] - myList[0])
	score += abs(magic['b'] - myList[1])
	score += abs(magic['c'] - myList[2])
	score += abs(magic['d'] - myList[3])
	score += abs(magic['e'] - myList[4])
	score += abs(magic['f'] - myList[5])
	score += abs(magic['g'] - myList[6])
	score += abs(magic['h'] - myList[7])
	score += abs(magic['i'] - myList[8])
	return score
lowestscore = ScoreFunc(m1)
if ScoreFunc(m2) < lowestscore:
	lowestscore = ScoreFunc(m2)
if ScoreFunc(m3) < lowestscore:
	lowestscore = ScoreFunc(m3)
if ScoreFunc(m3) < lowestscore:
	lowestscore = ScoreFunc(m3)
if ScoreFunc(m4) < lowestscore:
	lowestscore = ScoreFunc(m4)
if ScoreFunc(m5) < lowestscore:
	lowestscore = ScoreFunc(m5)
if ScoreFunc(m6) < lowestscore:
	lowestscore = ScoreFunc(m6)
if ScoreFunc(m7) < lowestscore:
	lowestscore = ScoreFunc(m7)
if ScoreFunc(m8) < lowestscore:
	lowestscore = ScoreFunc(m8)
print(lowestscore)


mat = []
for i in range(0, 3):
	mat.extend([int(x) for x in input().split()])
mag = [2, 9, 4, 7, 5, 3, 6, 1, 8]
min_cost = 1000

def rot(x):
	return [x[y] for y in [6, 3, 0, 7, 4, 1, 8, 5, 2]]

def mirr(x):
	return [x[y] for y in [2, 1, 0, 5, 4, 3, 8, 7, 6]]

def diff(x, y):
	return sum([abs(a - b) for (a, b) in zip(x, y)])
for r in range(1, 5):
	mag = rot(mag)
	min_cost = min(min_cost, diff(mag, mat))
	min_cost = min(min_cost, diff(mirr(mag), mat))
print(min_cost)


fElement = input().split()
fElement = [int(i) for i in fElement]
sElement = input().split()
sElement = [int(i) for i in sElement]
tElement = input().split()
tElement = [int(i) for i in tElement]
matrix = fElement + sElement + tElement
allSolutions = [[8, 1, 6, 3, 5, 7, 4, 9, 2], [6, 1, 8, 7, 5, 3, 2, 9, 4], [4, 3, 8, 9, 5, 1, 2, 7, 6], [2, 7, 6, 9, 5, 1, 4, 3, 8], [2, 9, 4, 7, 5, 3, 6, 1, 8], [4, 9, 2, 3, 5, 7, 8, 1, 6], [6, 7, 2, 1, 5, 9, 8, 3, 4], [8, 3, 4, 1, 5, 9, 6, 7, 2]]
i = 0
cost = 45
while i < 8:
	j = 0
	currentCost = 0
	while j < 9:
		currentCost = currentCost + abs(allSolutions[i][j] - matrix[j])
		j += 1
	if currentCost < cost:
		cost = currentCost
	i += 1
print(cost)


l1 = input()
l2 = input()
l3 = input()
inp = ' '.join([l1, l2, l3])
templates = ('8 1 6 3 5 7 4 9 2', '4 3 8 9 5 1 2 7 6', '2 9 4 7 5 3 6 1 8', '6 7 2 1 5 9 8 3 4', '6 1 8 7 5 3 2 9 4', '8 3 4 1 5 9 6 7 2', '4 9 2 3 5 7 8 1 6', '2 7 6 9 5 1 4 3 8')

def create_magic_square():
	diffs = []
	for t in templates:
		counter = 0
		if inp != t:
			for i in range(len(inp)):
				if inp[i] != t[i]:
					counter += abs(int(inp[i]) - int(t[i]))
			diffs.append(counter)
		else:
			return 0
	return min(diffs)
print(create_magic_square())


import sys

def rotate_matrix(m):
	return [[m[0][2], m[1][2], m[2][2]], [m[0][1], m[1][1], m[2][1]], [m[0][0], m[1][0], m[2][0]]]

def flip_matrix(m):
	return [[m[0][2], m[0][1], m[0][0]], [m[1][2], m[1][1], m[1][0]], [m[2][2], m[2][1], m[2][0]]]

def diff_between_matrices(bad_m, magic_m):
	cost_counter = 0
	for (i, row) in enumerate(bad_m):
		for (j, value) in enumerate(row):
			cost_counter += abs(value - magic_m[i][j])
	return cost_counter

def min_cost_matrix_permutations(matrix, new_matrix):
	costs = []
	costs.append(diff_between_matrices(matrix, new_matrix))
	for i in range(3):
		new_matrix = rotate_matrix(new_matrix)
		costs.append(diff_between_matrices(matrix, new_matrix))
	new_matrix = flip_matrix(new_matrix)
	costs.append(diff_between_matrices(matrix, new_matrix))
	for i in range(3):
		new_matrix = rotate_matrix(new_matrix)
		costs.append(diff_between_matrices(matrix, new_matrix))
	return min(costs)
matrix = []
for line in sys.stdin:
	row = [int(num.replace('\n', '')) for num in line.split(' ')]
	matrix.append(row)
new_matrix = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
cost = min_cost_matrix_permutations(matrix, new_matrix)
print(cost)


s = []
for i in range(3):
	s.append(list(map(int, input().split())))
min_cost = 1000
magic_squares = {1: [[2, 7, 6], [9, 5, 1], [4, 3, 8]], 2: [[4, 3, 8], [9, 5, 1], [2, 7, 6]], 3: [[4, 9, 2], [3, 5, 7], [8, 1, 6]], 4: [[8, 1, 6], [3, 5, 7], [4, 9, 2]], 5: [[8, 3, 4], [1, 5, 9], [6, 7, 2]], 6: [[6, 7, 2], [1, 5, 9], [8, 3, 4]], 7: [[6, 1, 8], [7, 5, 3], [2, 9, 4]], 8: [[2, 9, 4], [7, 5, 3], [6, 1, 8]]}
for n in range(1, 9):
	cost = 0
	ms = magic_squares[n]
	cost = sum((abs(s[i][j] - ms[i][j]) for i in range(3) for j in range(3)))
	min_cost = min(cost, min_cost)
print(min_cost)


import sys
mat = []
for _ in range(3):
	mat += [int(x) for x in input().split()]
squares = [[2, 7, 6, 9, 5, 1, 4, 3, 8], [2, 9, 4, 7, 5, 3, 6, 1, 8], [4, 3, 8, 9, 5, 1, 2, 7, 6], [4, 9, 2, 3, 5, 7, 8, 1, 6], [6, 1, 8, 7, 5, 3, 2, 9, 4], [6, 7, 2, 1, 5, 9, 8, 3, 4], [8, 1, 6, 3, 5, 7, 4, 9, 2], [8, 3, 4, 1, 5, 9, 6, 7, 2]]
cost = min([sum((abs(x - y) for (x, y) in zip(mat, sq))) for sq in squares])
print(cost)


magic_squares = [[4, 9, 2, 3, 5, 7, 8, 1, 6], [8, 1, 6, 3, 5, 7, 4, 9, 2], [2, 9, 4, 7, 5, 3, 6, 1, 8], [6, 1, 8, 7, 5, 3, 2, 9, 4], [4, 3, 8, 9, 5, 1, 2, 7, 6], [8, 3, 4, 1, 5, 9, 6, 7, 2], [2, 7, 6, 9, 5, 1, 4, 3, 8], [6, 7, 2, 1, 5, 9, 8, 3, 4]]
squares = []
for arr in [list(map(int, input().split(' '))) for x in range(3)]:
	for item in arr:
		squares.append(item)

def diff(my_arr, magic):
	my_diff = 0
	for (idx, val) in enumerate(my_arr):
		my_diff += abs(val - magic[idx])
	return my_diff

def min_diff():
	diffs = []
	for i in range(0, 8):
		diffs.append(diff(squares, magic_squares[i]))
	return min(diffs)
print(min_diff())


test_square = []
while 1:
	try:
		test_square.append([int(x) for x in input().strip().split()])
	except:
		break
test_square = [x for row in test_square for x in row]
magic_squares = [[2, 7, 6, 9, 5, 1, 4, 3, 8], [6, 7, 2, 1, 5, 9, 8, 3, 4], [4, 3, 8, 9, 5, 1, 2, 7, 6], [2, 9, 4, 7, 5, 3, 6, 1, 8], [8, 1, 6, 3, 5, 7, 4, 9, 2], [4, 9, 2, 3, 5, 7, 8, 1, 6], [8, 3, 4, 1, 5, 9, 6, 7, 2], [6, 1, 8, 7, 5, 3, 2, 9, 4]]
min_cost = 10 ** 6
for square in magic_squares:
	cost = 0
	for (i, element) in enumerate(square):
		cost += abs(element - test_square[i])
	if cost < min_cost:
		min_cost = cost
print(min_cost)


square = [[int(i) for i in input().split()] for i in range(3)]
square1 = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
square2 = [[2, 9, 4], [7, 5, 3], [6, 1, 8]]
square3 = [[8, 3, 4], [1, 5, 9], [6, 7, 2]]
square4 = [[6, 1, 8], [7, 5, 3], [2, 9, 4]]
square5 = [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
square6 = [[4, 3, 8], [9, 5, 1], [2, 7, 6]]
square7 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
square8 = [[6, 7, 2], [1, 5, 9], [8, 3, 4]]
squares = [square1, square2, square3, square4, square5, square6, square7, square8]
mindiff = 1000
for i in squares:
	diff = 0
	for j in range(3):
		for k in range(3):
			diff += abs(square[j][k] - i[j][k])
	if mindiff > diff:
		mindiff = diff
print(mindiff)


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)


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)


row1 = list(map(int, input().strip().split()))
row2 = list(map(int, input().strip().split()))
row3 = list(map(int, input().strip().split()))
rows = [row1, row2, row3]
squares = []
squares.append([[8, 1, 6], [3, 5, 7], [4, 9, 2]])
squares.append([[4, 3, 8], [9, 5, 1], [2, 7, 6]])
squares.append([[2, 9, 4], [7, 5, 3], [6, 1, 8]])
squares.append([[6, 7, 2], [1, 5, 9], [8, 3, 4]])
squares.append([[6, 1, 8], [7, 5, 3], [2, 9, 4]])
squares.append([[8, 3, 4], [1, 5, 9], [6, 7, 2]])
squares.append([[4, 9, 2], [3, 5, 7], [8, 1, 6]])
squares.append([[2, 7, 6], [9, 5, 1], [4, 3, 8]])
totals = []
for k in range(8):
	total = 0
	for i in range(3):
		for j in range(3):
			total += abs(squares[k][i][j] - rows[i][j])
	totals.append(total)
print(min(totals))


import math, sys
a_ms = [[8, 1, 6, 3, 5, 7, 4, 9, 2], [6, 1, 8, 7, 5, 3, 2, 9, 4], [4, 3, 8, 9, 5, 1, 2, 7, 6], [2, 7, 6, 9, 5, 1, 4, 3, 8], [2, 9, 4, 7, 5, 3, 6, 1, 8], [4, 9, 2, 3, 5, 7, 8, 1, 6], [6, 7, 2, 1, 5, 9, 8, 3, 4], [8, 3, 4, 1, 5, 9, 6, 7, 2]]

def func(A, B):
	return [abs(a - b) for (a, b) in zip(A, B) if abs(a - b) >= 0]
t = []
for line in sys.stdin:
	t += [i for i in line if i in [str(i) for i in range(10)]]
	t = [int(i) for i in t]
l = []
min_ = 100
for i in a_ms:
	if min_ > sum(func(t, i)):
		min_ = sum(func(t, i))
print(min_)


u = []
for i in range(3):
	u += list(map(int, input().strip().split()))[:3]
l = []
l.append([4, 9, 2, 3, 5, 7, 8, 1, 6])
l.append([8, 1, 6, 3, 5, 7, 4, 9, 2])
l.append([2, 9, 4, 7, 5, 3, 6, 1, 8])
l.append([4, 3, 8, 9, 5, 1, 2, 7, 6])
l.append([6, 7, 2, 1, 5, 9, 8, 3, 4])
l.append([8, 3, 4, 1, 5, 9, 6, 7, 2])
l.append([6, 1, 8, 7, 5, 3, 2, 9, 4])
l.append([2, 7, 6, 9, 5, 1, 4, 3, 8])
m = None
for i in l:
	s = 0
	for j in range(9):
		s += abs(i[j] - u[j])
	if m == None:
		m = s
	elif s < m:
		m = s
print(m)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 27
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


from itertools import *
X = []
X.extend(list(map(int, input().split())))
X.extend(list(map(int, input().split())))
X.extend(list(map(int, input().split())))
Ans = 81
for P in permutations(range(1, 10)):
	if sum(P[0:3]) == 15 and sum(P[3:6]) == 15 and (sum(P[0::3]) == 15) and (sum(P[1::3]) == 15) and (P[0] + P[4] + P[8] == 15) and (P[2] + P[4] + P[6] == 15):
		Ans = min(Ans, sum((abs(P[i] - X[i]) for i in range(0, 9))))
print(Ans)


possibilities = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
a = []
a.append([int(a_temp) for a_temp in input().strip().split(' ')])
a.append([int(a_temp) for a_temp in input().strip().split(' ')])
a.append([int(a_temp) for a_temp in input().strip().split(' ')])
mincost = 81
for itm in possibilities:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(a[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


arrs = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]

def diff(arr0, arr1):
	ret = 0
	for i in range(3):
		for j in range(3):
			ret += abs(arr0[i][j] - arr1[i][j])
	return ret
mat = []
for _ in range(3):
	mat.append(list(map(int, input().strip().split(' '))))
ret = None
for arr in arrs:
	d = diff(mat, arr)
	if ret == None or d < ret:
		ret = d
print(ret)


def rotated_grid(grid):
	return [[grid[2][0], grid[1][0], grid[0][0]], [grid[2][1], grid[1][1], grid[0][1]], [grid[2][2], grid[1][2], grid[0][2]]]

def vertical_symed(grid):
	return [[grid[0][2], grid[0][1], grid[0][0]], [grid[1][2], grid[1][1], grid[1][0]], [grid[2][2], grid[2][1], grid[2][0]]]

def diagonal_symed(grid):
	return [[grid[0][0], grid[1][0], grid[2][0]], [grid[0][1], grid[1][1], grid[2][1]], [grid[0][2], grid[1][2], grid[2][2]]]

def diagonal_symed2(grid):
	return [[grid[2][2], grid[1][2], grid[0][2]], [grid[2][1], grid[1][1], grid[0][1]], [grid[2][0], grid[1][0], grid[0][0]]]
grid = [list(map(int, input().strip().split(' '))) for i in range(3)]

def grid_dist(grid1, grid2):
	cpt = 0
	for i in range(3):
		for j in range(3):
			cpt += abs(grid1[i][j] - grid2[i][j])
	return cpt
magic_squares = [[[4, 9, 2], [3, 5, 7], [8, 1, 6]]]
for i in range(3):
	magic_squares.append(rotated_grid(magic_squares[i]))
magic_squares.append([magic_squares[0][2], magic_squares[0][1], magic_squares[0][0]])
magic_squares.append(vertical_symed(magic_squares[0]))
magic_squares.append(diagonal_symed(magic_squares[0]))
magic_squares.append(diagonal_symed2(magic_squares[0]))
retour = float('Inf')
for magic in magic_squares:
	if grid_dist(magic, grid) < retour:
		retour = grid_dist(magic, grid)
print(retour)


a = list(map(int, input().strip().split()))
a.extend(map(int, input().strip().split()))
a.extend(map(int, input().strip().split()))
magicSquares = [[2, 7, 6, 9, 5, 1, 4, 3, 8], [2, 9, 4, 7, 5, 3, 6, 1, 8], [4, 3, 8, 9, 5, 1, 2, 7, 6], [4, 9, 2, 3, 5, 7, 8, 1, 6], [6, 1, 8, 7, 5, 3, 2, 9, 4], [6, 7, 2, 1, 5, 9, 8, 3, 4], [8, 3, 4, 1, 5, 9, 6, 7, 2], [8, 1, 6, 3, 5, 7, 4, 9, 2]]
minCost = 72
for mS in magicSquares:
	cost = sum([abs(a[i] - mS[i]) for i in range(9)])
	if cost < minCost:
		minCost = cost
print(minCost)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


from sys import stdin

def readInts():
	return map(int, stdin.readline().strip().split())

def rotateCW(sq):
	sq2 = [[0 for _ in range(3)] for _ in range(3)]
	for r in range(3):
		for c in range(3):
			sq2[r][c] = sq[2 - c][r]
	return sq2

def flip(sq):
	sq2 = [[0 for _ in range(3)] for _ in range(3)]
	for r in range(3):
		for c in range(3):
			sq2[r][c] = sq[r][2 - c]
	return sq2
msq = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]

def diff(sq1, sq2):
	out = 0
	for r in range(3):
		for c in range(3):
			out += abs(sq1[r][c] - sq2[r][c])
	return out

def find_min(msq, sq):
	msq_flip = flip(msq)
	msq_rot = msq
	best = min(diff(msq_flip, sq), diff(msq_rot, sq))
	for _ in range(3):
		msq_flip = rotateCW(msq_flip)
		msq_rot = rotateCW(msq_rot)
		best = min([best, diff(msq_flip, sq), diff(msq_rot, sq)])
	return best
sq = []
for _ in range(3):
	sq.append(list(readInts()))
print(find_min(msq, sq))


M = []
for _ in (0, 1, 2):
	M += [int(x) for x in input().split()]
print(min((sum((abs(M[i] - C[i]) for i in range(9))) for C in [[8, 1, 6, 3, 5, 7, 4, 9, 2], [6, 1, 8, 7, 5, 3, 2, 9, 4], [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]])))


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


inp = []
for i in range(3):
	temp = [int(i) for i in input().split(' ')]
	inp.append(temp)
perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
mincost = 100
for matrix in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(inp[i][j] - matrix[i][j])
	if curr < mincost:
		mincost = curr
print(mincost)


l1 = (((4, 9, 2), (3, 5, 7), (8, 1, 6)), ((4, 3, 8), (9, 5, 1), (2, 7, 6)), ((2, 7, 6), (9, 5, 1), (4, 3, 8)), ((2, 9, 4), (7, 5, 3), (6, 1, 8)), ((6, 1, 8), (7, 5, 3), (2, 9, 4)), ((6, 7, 2), (1, 5, 9), (8, 3, 4)), ((8, 1, 6), (3, 5, 7), (4, 9, 2)), ((8, 3, 4), (1, 5, 9), (6, 7, 2)))
l = [list(map(int, input().split())) for _ in range(3)]
m = 1000000
for item in l1:
	count = 0
	for i in range(3):
		for j in range(3):
			if l[i][j] != item[i][j]:
				count += abs(l[i][j] - item[i][j])
	m = min(m, count)
print(m)


def dist(a, b):
	return sum((abs(x - y) for (x, y) in zip(a, b)))
inp = [list(map(int, input().split())) for _ in range(3)]
(A, B) = ([0] * 9, [0] * 9)
for i in range(3):
	A[i * 3:i * 3 + 3] = inp[i]
res = 9999999
for n in range(1000, 10000):
	(B[0], B[1], B[2], B[3], B[4]) = (n // 1000, n // 100 % 10, n // 10 % 10, 0, n % 10)
	if len(set(B[:5])) != 5:
		continue
	s = sum(B[:3])
	B[8] = s - (B[0] + B[4])
	if not 1 <= B[8] <= 9 or B[8] in B[:5]:
		continue
	B[5] = s - (B[2] + B[8])
	if not 1 <= B[5] <= 9 or B[5] in B[:5] or B[5] == B[8]:
		continue
	B[3] = s - (B[4] + B[5])
	if not 1 <= B[3] <= 9 or B[3] in B[:3] or B[3] in (B[4], B[5], B[8]):
		continue
	B[6] = s - (B[0] + B[3])
	if not 1 <= B[6] <= 9 or B[6] in B[:6] or B[6] == B[8]:
		continue
	B[7] = s - (B[1] + B[4])
	if not 1 <= B[7] <= 9 or B[7] in B[:7] or B[7] == B[8]:
		continue
	if any((sum(B[i:i + 3]) != s for i in range(0, 9, 3))):
		continue
	if any((B[i] + B[i + 3] + B[i + 6] != s for i in range(3))):
		continue
	if B[0] + B[4] + B[8] != s or B[2] + B[4] + B[6] != s:
		continue
	d = dist(A, B)
	res = min(d, res)
print(res)


square = [int(i) for i in input().strip().split()] + [int(i) for i in input().strip().split()] + [int(i) for i in input().strip().split()]
known_squares = [[8, 1, 6, 3, 5, 7, 4, 9, 2], [6, 1, 8, 7, 5, 3, 2, 9, 4], [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], [2, 7, 6, 9, 5, 1, 4, 3, 8], [4, 9, 2, 3, 5, 7, 8, 1, 6], [2, 9, 4, 7, 5, 3, 6, 1, 8]]
min_cost = 81
for sq in known_squares:
	cost = sum([abs(i[0] - i[1]) for i in zip(sq, square)])
	min_cost = min(min_cost, cost)
print(min_cost)


all_magics = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
m = [[int(x) for x in input().strip().split(' ')] for i in range(3)]
min_cost = 1000000
for magic in all_magics:
	cost = 0
	for i in range(3):
		for j in range(3):
			cost += abs(magic[i][j] - m[i][j])
	if cost < min_cost:
		min_cost = cost
print(min_cost)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


from itertools import permutations

def all_matrizes():
	for x in permutations(range(1, 10)):
		if not sum(x[:3]) == sum(x[3:6]) == sum(x[6:]):
			continue
		if not sum(x[::3]) == sum(x[1::3]) == sum(x[2::3]):
			continue
		if x[0] + x[4] + x[8] != 15 or x[2] + x[4] + x[6] != 15:
			continue
		yield x

def difference(a, m):
	return sum((abs(a[i] - m[i]) for i in range(9)))
a = [int(i) for i in (input() + ' ' + input() + ' ' + input()).split()]
print(min((difference(i, a) for i in all_matrizes())))


X = [[0 for i in range(3)] for i in range(3)]
T = input()
i = 0
while T:
	for (id, j) in enumerate(T.split(' ')):
		X[i][id] = int(j)
	try:
		T = input()
	except:
		break
	i += 1
perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]

def diff(A, B):
	s = 0
	for i in range(3):
		for j in range(3):
			s += abs(A[i][j] - B[i][j])
	return s
print(min([diff(X, perm[i]) for i in range(8)]))


from itertools import permutations
magicsq = tuple((s for s in permutations(range(1, 10)) if sum(s[0:3]) == sum(s[3:6]) == sum(s[6:9]) == sum(s[::3]) == sum(s[1::3]) == sum(s[2::3]) == sum(s[::4]) == sum(s[2:7:2])))
ar = sum((tuple(map(int, input().split())) for _ in range(3)), ())
print(min((sum((abs(x - y) for (x, y) in zip(s, ar))) for s in magicsq)))


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


comb = [[[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]]]
rows = [list(map(int, input().split())) for _ in range(3)]
m = 41
for c in comb:
	a = 0
	for i in range(3):
		for j in range(3):
			a += abs(c[i][j] - rows[i][j])
	m = min(m, a)
print('{:d}'.format(m))


import sys
s = []
(x1, x2, x3) = input().strip().split(' ')
s.append([int(x1), int(x2), int(x3)])
(x1, x2, x3) = input().strip().split(' ')
s.append([int(x1), int(x2), int(x3)])
(x1, x2, x3) = input().strip().split(' ')
s.append([int(x1), int(x2), int(x3)])

class MagicSquare:

	def __init__(self, s):
		self.matrix = s
		self.score = 0

	def is_magic(self):
		return self.are_rows_magic() and self.are_cols_magic() and self.are_diags_magic()

	def are_rows_magic(self):
		return sum(self.matrix[0]) == 15 and sum(self.matrix[1]) == 15 and (sum(self.matrix[2]) == 15)

	def are_cols_magic(self):
		col0 = sum([self.matrix[0][0], self.matrix[1][0], self.matrix[2][0]])
		col1 = sum([self.matrix[0][1], self.matrix[1][1], self.matrix[2][1]])
		col2 = sum([self.matrix[0][2], self.matrix[1][2], self.matrix[2][2]])
		return col0 == 15 and col1 == col0 and (col2 == col0)

	def are_diags_magic(self):
		diag0 = sum([self.matrix[0][0], self.matrix[1][1]], self.matrix[2][2])
		diag1 = sum([self.matrix[2][0], self.matrix[1][1], self.matrix[0][2]])
		return diag0 == 15 and diag1 == diag0

	def print_matrix(self):
		for r in self.matrix:
			print('{}  {}  {}  | {}'.format(r[0], r[1], r[2], r[0] + r[1] + r[2]))
		print('----------')
		print('{} {} {}'.format(self.matrix[0][0] + self.matrix[1][0] + self.matrix[2][0], self.matrix[0][1] + self.matrix[1][1] + self.matrix[2][1], self.matrix[0][2] + self.matrix[1][2] + self.matrix[2][2]))

	def change_cell(self, r, c, v):
		old_val = self.matrix[r][c]
		self.matrix[r][c] = v
		self.score = self.score + abs(old_val - v)

	def get_corner_values(self):
		return [self.matrix[0][0], self.matrix[0][2], self.matrix[2][0], self.matrix[2][2]]

	def is_good_corner(self, r, c):
		return self.matrix[r][c] in [2, 4, 6, 8]

	def get_opposite_square(self, r, c):
		if r == 1 and c == 1:
			return None
		else:
			return (2 - r, 2 - c)

	def get_remaining_corner_vals(self):
		vals = [2, 4, 6, 8]
		current_vals = self.get_corner_values()
		for current_val in current_vals:
			if current_val in vals:
				vals.remove(current_val)
		return vals

	def find_best_replacement(self):
		all_possible_matrices = [[[2, 7, 6], [9, 5, 1], [4, 3, 8]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[8, 1, 6], [3, 5, 7], [4, 9, 2]]]
		test_score = 999
		for m in all_possible_matrices:
			iter_score = self.check_score(m)
			if iter_score < test_score:
				test_score = iter_score
				best_matrix = []
				for i in range(0, 3):
					best_matrix.append(list(m[i]))
		return best_matrix

	def find_best_corner_score(self):
		all_possible_corners = [[2, 6, 4, 8], [2, 4, 6, 8], [4, 8, 2, 6], [4, 2, 8, 6], [6, 2, 8, 4], [6, 8, 2, 4], [8, 4, 6, 2], [8, 6, 4, 2]]
		test_score = 999
		new_matrix = []
		for i in range(0, 3):
			new_matrix.append(list(self.matrix[i]))
		for test in all_possible_corners:
			new_matrix[0][0] = test[0]
			new_matrix[0][2] = test[1]
			new_matrix[2][0] = test[2]
			new_matrix[2][2] = test[3]
			iter_score = self.check_score(new_matrix)
			if iter_score < test_score:
				test_score = iter_score
				best_matrix = []
				for i in range(0, 3):
					best_matrix.append(list(new_matrix[i]))
		return (best_matrix[0][0], best_matrix[0][2], best_matrix[2][0], best_matrix[2][2])

	def check_score(self, new_matrix):
		score = 0
		for i in range(0, 3):
			for j in range(0, 3):
				score = score + abs(self.matrix[i][j] - new_matrix[i][j])
		return score

	def find_issues(self):
		if self.matrix[1][1] != 5:
			self.change_cell(1, 1, 5)
			if self.is_magic():
				return self.score
		(c1, c2, c3, c4) = self.find_best_corner_score()
		self.change_cell(0, 0, c1)
		self.change_cell(0, 2, c2)
		self.change_cell(2, 0, c3)
		self.change_cell(2, 2, c4)
		corner1 = self.matrix[0][0]
		corner2 = self.matrix[0][2]
		corner3 = self.matrix[2][0]
		corner4 = self.matrix[2][2]
		if self.matrix[0][1] != 15 - (corner1 + corner2):
			self.change_cell(0, 1, 15 - (corner1 + corner2))
		if self.matrix[1][0] != 15 - (corner1 + corner3):
			self.change_cell(1, 0, 15 - (corner1 + corner3))
		if self.matrix[1][2] != 15 - (corner2 + corner4):
			self.change_cell(1, 2, 15 - (corner2 + corner4))
		if self.matrix[2][1] != 15 - (corner3 + corner4):
			self.change_cell(2, 1, 15 - (corner3 + corner4))
		return self.score
tester = MagicSquare(s)
replacement = tester.find_best_replacement()
for i in range(0, 3):
	for j in range(0, 3):
		tester.change_cell(i, j, replacement[i][j])
print(tester.score)


a = [0] * 3
for i in range(3):
	a[i] = [int(i) for i in input().strip().split(' ')]
c = list()
b = ((8, 1, 6, 3, 5, 7, 4, 9, 2), (6, 1, 8, 7, 5, 3, 2, 9, 4), (4, 9, 2, 3, 5, 7, 8, 1, 6), (2, 9, 4, 7, 5, 3, 6, 1, 8), (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), (2, 7, 6, 9, 5, 1, 4, 3, 8))
for i in range(3):
	for j in range(3):
		c.append(a[i][j])
diff = list()
for i in range(8):
	s = 0
	for j in range(9):
		s += abs(c[j] - b[i][j])
	diff.append(s)
print(min(diff))


li = []
for i in range(3):
	lj = [int(x) for x in input().split()]
	li.append(lj)
	lj = list()
possib = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
total = []
total_list = []
for each_matr in possib:
	for x in range(3):
		for y in range(3):
			total.append(abs(each_matr[x][y] - li[x][y]))
	total_list.append(sum(total))
	total = []
print(min(total_list))


V = ((8, 1, 6, 3, 5, 7, 4, 9, 2), (6, 1, 8, 7, 5, 3, 2, 9, 4), (4, 9, 2, 3, 5, 7, 8, 1, 6), (2, 9, 4, 7, 5, 3, 6, 1, 8), (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), (2, 7, 6, 9, 5, 1, 4, 3, 8))
M = tuple(map(int, (input() + ' ' + input() + ' ' + input()).split()))
print(min((sum(map(lambda x: abs(x[0] - x[1]), zip(v, M))) for v in V)))


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


import sys

def distance(b, a=tuple(map(int, sys.stdin.read().split()))):
	return sum((abs(ai - bi) for (ai, bi) in zip(a, b)))
magic = ['618753294', '816357492', '672159834', '834159672', '294753618', '492357816', '276951438', '438951276']
print(min(map(distance, (tuple(map(int, m)) for m in magic))))


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


G = [list(map(int, input().split())) for _ in range(3)]
M = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
res = 100
for m in M:
	s = 0
	for (s1, s2) in zip(m, G):
		for i in map(lambda t: abs(t[0] - t[1]), zip(s1, s2)):
			s += i
	res = min(res, s)
print(res)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


s = []
s.append([int(i) for i in input().split(' ')])
s.append([int(i) for i in input().split(' ')])
s.append([int(i) for i in input().split(' ')])
magic = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
mn = 81
for ms in magic:
	dum_cost = 0
	for row in range(len(ms)):
		for elem in range(len(ms[row])):
			dum_cost += abs(s[row][elem] - ms[row][elem])
	if dum_cost < mn:
		mn = dum_cost
print(mn)


import itertools
import random
x = []
x.extend(list(map(int, input().split())))
x.extend(list(map(int, input().split())))
x.extend(list(map(int, input().split())))

def is_magic(s):
	if sum(s[:3]) != 15:
		return False
	elif sum(s[3:6]) != 15:
		return False
	elif sum(s[6:9]) != 15:
		return False
	elif sum(s[::3]) != 15:
		return False
	elif sum(s[1::3]) != 15:
		return False
	elif sum(s[2::3]) != 15:
		return False
	elif s[0] + s[4] + s[8] != 15:
		return False
	elif s[2] + s[4] + s[6] != 15:
		return False
	else:
		return True
minCost = 81
for p in itertools.permutations(range(1, 10)):
	s = list(p)
	if is_magic(s):
		newCost = sum((abs(x[i] - s[i]) for i in range(0, 9)))
		if newCost < minCost:
			minCost = newCost
print(minCost)


config = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in config:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)


def diff(test, correct):
	return sum((abs(correct[x][y] - test[x][y]) for x in range(3) for y in range(3)))
magic = [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
magics = [magic]
temp = magic
for i in range(3):
	temp = list(zip(*temp[::-1]))
	magics.append(temp)
temp = [x[::-1] for x in magic]
magics.append(temp)
for i in range(3):
	temp = list(zip(*temp[::-1]))
	magics.append(temp)
square = [list(map(int, input().split())), list(map(int, input().split())), list(map(int, input().split()))]
print(min((diff(square, correct) for correct in magics)))


def abs(x):
	if x < 0:
		x = -x
	return x

def cost(m_sq, sq):
	cost = 0
	for i in range(3):
		for j in range(3):
			cost += abs(m_sq[i][j] - sq[i][j])
	return cost
magic_squares = [[[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[8, 1, 6], [3, 5, 7], [4, 9, 2]]]
square = []
for i in range(3):
	row = input().split()
	square.append([int(row[0]), int(row[1]), int(row[2])])
min_cost = cost(magic_squares[0], square)
for i in range(1, 8):
	current_cost = cost(magic_squares[i], square)
	if min_cost > current_cost:
		min_cost = current_cost
print(min_cost)


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
s = [0, 0, 0]
for i in range(3):
	s[i] = [int(temp) for temp in input().split(' ')]
min_cost = 0
for i in range(3):
	for j in range(3):
		min_cost += abs(perm[0][i][j] - s[i][j])
for i in range(1, 8):
	cost = 0
	for j in range(3):
		for k in range(3):
			cost += abs(perm[i][j][k] - s[j][k])
	if min_cost > cost:
		min_cost = cost
print(min_cost)


lis = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
data = []
cost = []
for _ in range(3):
	data.append([int(x) for x in input().split(' ')])
for i in lis:
	c = 0
	for l in range(3):
		c = c + abs(i[l][0] - data[l][0])
		c = c + abs(i[l][1] - data[l][1])
		c = c + abs(i[l][2] - data[l][2])
	cost.append(c)
print(min(cost))


perm = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], [[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]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
matrix = []
for i in range(3):
	matrix.append(list(map(int, input().strip().split(' '))))
mincost = 81
for itm in perm:
	curr = 0
	for i in range(3):
		for j in range(3):
			curr += abs(matrix[i][j] - itm[i][j])
	mincost = min(curr, mincost)
print(mincost)