# Enter your code here. Read input from STDIN. Print output to STDOUT a=[] mag1=[[[2,9,4],[7,5,3],[6,1,8]],[[2,7,6],[9,5,1],[4,3,8]],[[8,3,4],[1,5,9],[6,7,2]],[[8,1,6],[3,5,7],[4,9,2]], [[4,9,2],[3,5,7],[8,1,6]],[[6,7,2],[1,5,9],[8,3,4]],[[4,3,8],[9,5,1],[2,7,6]],[[6,1,8],[7,5,3],[2,9,4]]] for i in range(0,3): a.append(raw_input().split()) for i in range(0,3): for j in range(0,3): a[i][j]=int(a[i][j]) cost=[0 for x in range(8)] for i in range(0,8): for j in range(0,3): for k in range(0,3): cost[i]+=abs(mag1[i][j][k]-a[j][k]) print min(cost)