# Enter your code here. Read input from STDIN. Print output to STDOUT import sys a = [] for a_i in xrange(3): a_temp = map(int,raw_input().strip().split(' ')) a.append(a_temp) L1 = [[2, 7, 6], [9, 5, 1], [4, 3, 8]] L2 = [[4, 3, 8], [9, 5, 1], [2, 7, 6]] L3 = [[6, 1, 8], [7, 5, 3], [2, 9, 4]] L4 = [[8, 3, 4], [1, 5, 9], [6, 7, 2]] L5 = [[6, 7, 2], [1, 5, 9], [8, 3, 4]] L6 = [[4, 9, 2], [3, 5, 7], [8, 1, 6]] L7 = [[2, 9, 4], [7, 5, 3], [6, 1, 8]] L8 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]] l1, l2, l3, l4, l5, l6, l7, l8 = 0, 0, 0, 0, 0, 0, 0, 0 for i in range(3): for j in range(3): l1 += abs(L1[i][j] - a[i][j]) l2 += abs(L2[i][j] - a[i][j]) l3 += abs(L3[i][j] - a[i][j]) l4 += abs(L4[i][j] - a[i][j]) l5 += abs(L5[i][j] - a[i][j]) l6 += abs(L6[i][j] - a[i][j]) l7 += abs(L7[i][j] - a[i][j]) l8 += abs(L8[i][j] - a[i][j]) print min(l1, l2, l3, l4, l5, l6, l7, l8)