def compare(sq1, sq2): result = 0 for i in range(3): for j in range(3): result += abs(sq1[i][j] - sq2[i][j]) return result ms = [] ms.append([[8,1,6],[3,5,7],[4,9,2]]) ms.append([[4,3,8],[9,5,1],[2,7,6]]) ms.append([[2,9,4],[7,5,3],[6,1,8]]) ms.append([[6,7,2],[1,5,9],[8,3,4]]) ms.append([[6,1,8],[7,5,3],[2,9,4]]) ms.append([[8,3,4],[1,5,9],[6,7,2]]) ms.append([[4,9,2],[3,5,7],[8,1,6]]) ms.append([[2,7,6],[9,5,1],[4,3,8]]) sq1 = [[int(i) for i in input().split()] for n in range(3)] min_cost = 1000000 for sq2 in ms: cost = compare(sq1, sq2) if cost < min_cost: min_cost = cost if min_cost == 0: break print(min_cost)