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)