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