import sys def all_equivalent(m): def all_reversed(m): yield m yield reversed(m) def hor_reversed(rows): return map(reversed, rows) yield hor_reversed(m) yield hor_reversed(reversed(m)) for r in all_reversed(m): yield r for r in all_reversed([list(t) for t in zip(*m)]): yield r def cost(s, m): ss = (si for row in s for si in row) ms = (mi for row in m for mi in row) return sum(abs(si - mi) for si, mi in zip(ss, ms)) magic = [[4, 9, 2], [3, 5, 7], [8, 1, 6]] all_magic = all_equivalent(magic) s = [list(map(int, line.split())) for line in sys.stdin.readlines()] print(min(cost(s, m) for m in all_magic))