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