# Enter your code here. Read input from STDIN. Print output to STDOUT import sys input = sys.stdin.readlines() test = [[int(y) for y in x if y.isdigit()] for x in input] costs = [] squares = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]], [[6, 1, 8], [7, 5, 3], [2, 9, 4]], [[8, 3, 4], [1, 5, 9], [6, 7, 2]], [[4, 3, 8], [9, 5, 1], [2, 7, 6]], [[6, 7, 2], [1, 5, 9], [8, 3, 4]], [[2, 7, 6], [9, 5, 1], [4, 3, 8]], [[4, 9, 2], [3, 5, 7], [8, 1, 6]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]]] for i in squares: current_cost = 0 current_cost += (abs(test[0][0]-i[0][0])) current_cost += (abs(test[0][1]-i[0][1])) current_cost += (abs(test[0][2]-i[0][2])) current_cost += (abs(test[1][0]-i[1][0])) current_cost += (abs(test[1][1]-i[1][1])) current_cost += (abs(test[1][2]-i[1][2])) current_cost += (abs(test[2][0]-i[2][0])) current_cost += (abs(test[2][1]-i[2][1])) current_cost += (abs(test[2][2]-i[2][2])) costs.append(current_cost) print min(costs)