row=list() for i in range(3): temp=list(input().split()) temp=[int(x) for x in temp] row.append(temp) cost=0 cost+=abs(5-row[1][1]) a=abs(4-row[0][0])+abs(9-row[0][1])+abs(2-row[0][2])+abs(3-row[1][0])+abs(7-row[1][2])+abs(8-row[2][0])+abs(1-row[2][1])+abs(6-row[2][2]) b=abs(2-row[0][0])+abs(7-row[0][1])+abs(6-row[0][2])+abs(9-row[1][0])+abs(1-row[1][2])+abs(4-row[2][0])+abs(3-row[2][1])+abs(8-row[2][2]) c=abs(6-row[0][0])+abs(1-row[0][1])+abs(8-row[0][2])+abs(7-row[1][0])+abs(3-row[1][2])+abs(2-row[2][0])+abs(9-row[2][1])+abs(4-row[2][2]) d=abs(8-row[0][0])+abs(3-row[0][1])+abs(4-row[0][2])+abs(1-row[1][0])+abs(9-row[1][2])+abs(6-row[2][0])+abs(7-row[2][1])+abs(2-row[2][2]) e=abs(2-row[0][0])+abs(9-row[0][1])+abs(4-row[0][2])+abs(7-row[1][0])+abs(3-row[1][2])+abs(6-row[2][0])+abs(1-row[2][1])+abs(8-row[2][2]) f=abs(4-row[0][0])+abs(3-row[0][1])+abs(8-row[0][2])+abs(9-row[1][0])+abs(1-row[1][2])+abs(2-row[2][0])+abs(7-row[2][1])+abs(6-row[2][2]) g=abs(8-row[0][0])+abs(1-row[0][1])+abs(6-row[0][2])+abs(3-row[1][0])+abs(7-row[1][2])+abs(4-row[2][0])+abs(9-row[2][1])+abs(2-row[2][2]) h=abs(6-row[0][0])+abs(7-row[0][1])+abs(2-row[0][2])+abs(1-row[1][0])+abs(9-row[1][2])+abs(8-row[2][0])+abs(3-row[2][1])+abs(4-row[2][2]) cost+=min(a,b,c,d,e,f,g,h) print(cost)