import sys
line1=[int(word) for word in input().strip().split(' ')];
line2=[int(word) for word in input().strip().split(' ')];
line3=[int(word) for word in input().strip().split(' ')];
line=line1+line2+line3
magicmatrixes=[
[2,7,6,9,5,1,4,3,8],
[2,9,4,7,5,3,6,1,8],
[4,3,8,9,5,1,2,7,6],
[4,9,2,3,5,7,8,1,6],
]
cost=100
for i in magicmatrixes:
    cost=min(cost,sum(abs(line[j]-i[j]) for j in range(9)))
    cost=min(cost,sum(abs(line[j]-i[8-j]) for j in range(9)))
print(cost)