# Enter your code here. Read input from STDIN. Print output to STDOUT def cost(s1,s2): cost_value=0 for i in xrange(3): for j in xrange(3): cost_value = cost_value + abs(s1[i][j]-s2[i][j]) return cost_value square=[] for i in xrange(3): inputas=map(int,raw_input().strip().split()) square.append(inputas) magics={1:[[8, 1 ,6],[3, 5, 7],[4, 9, 2]],2:[[4, 3, 8],[9, 5, 1],[2, 7, 6]],3:[[2, 9, 4],[7, 5, 3],[6, 1, 8]], 4:[[6, 7, 2],[1, 5, 9],[8, 3, 4]],5:[[6, 1, 8],[7, 5, 3],[2, 9, 4]],6:[[2, 7, 6],[9, 5, 1],[4, 3 ,8]], 7:[[4, 9, 2],[3, 5, 7],[8, 1, 6]],8:[[8, 3, 4],[1, 5, 9],[6, 7, 2]]} costs=[] for each in xrange(1,9): costs.append(cost(square,magics[each])) print min(costs)