def readints(): return list(map(int, input().strip().split())) def transpose(m): return list(zip(*m)) def reverseRows(m): return [list(reversed(r)) for r in m] def rotate90(m): return reverseRows(transpose(m)) magic = [ [8, 1, 6], [3, 5, 7], [4, 9, 2] ] no_reflections = [ magic, rotate90(magic), rotate90(rotate90(magic)), rotate90(rotate90(rotate90(magic))) ] all_magic = no_reflections + [reverseRows(m) for m in no_reflections] def read_matrix(): return [readints() for _ in range(3)] def matrix_diff(m1, m2): err = 0 for row1, row2 in zip(m1, m2): for a, b in zip(row1, row2): err += abs(a - b) return err def main(): matrix = read_matrix() print(min(matrix_diff(magic, matrix) for magic in all_magic)) main()