mat = [] for i in range(0,3): mat.extend([int(x) for x in input().split()]) # magic square base form mag = [2,9,4,7,5,3,6,1,8] min_cost = 1000 def rot(x): return [x[y] for y in [6,3,0,7,4,1,8,5,2]] def mirr(x): return [x[y] for y in [2,1,0,5,4,3,8,7,6]] def diff(x,y): return sum([abs(a-b) for a,b in zip(x,y)]) for r in range(1,5): mag = rot(mag) min_cost = min(min_cost, diff(mag, mat)) min_cost = min(min_cost, diff(mirr(mag), mat)) print(min_cost)