# Enter your code here. Read input from STDIN. Print output to STDOUT mt = [[],[],[]] mt[0] = map(int,raw_input().split()) mt[1] = map(int,raw_input().split()) mt[2] = map(int,raw_input().split()) mgs = [[[],[],[]] for i in range(0,8)] mgs[0] = [[8,1,6], [3,5,7], [4,9,2]] mgs[1] = [[4,9,2], [3,5,7], [8,1,6]] mgs[2] = [[6,1,8], [7,5,3], [2,9,4]] mgs[3] = [[2,9,4], [7,5,3], [6,1,8]] mgs[4] = [[8,3,4], [1,5,9], [6,7,2]] mgs[5] = [[6,7,2], [1,5,9], [8,3,4]] mgs[6] = [[4,3,8], [9,5,1], [2,7,6]] mgs[7] = [[2,7,6], [9,5,1], [4,3,8]] #compare given matrix with all the 8 magic squares and see which one can have the least difference difference = [] for t in range(0,8): current = 0 for i in range(0,3): for j in range(0,3): current = current + abs(mt[i][j]-mgs[t][i][j]) difference.append(current) print(min(difference))