ms1 = [[8,1,6],[3,5,7],[4,9,2]] ms2 = [[8,3,4],[1,5,9],[6,7,2]] ms3 = [[6,7,2],[1,5,9],[8,3,4]] ms4 = [[4,9,2],[3,5,7],[8,1,6]] ms5 = [[2,9,4],[7,5,3],[6,1,8]] ms6 = [[2,7,6],[9,5,1],[4,3,8]] ms7 = [[4,3,8],[9,5,1],[2,7,6]] ms8 = [[6,1,8],[7,5,3],[2,9,4]] #print(ms1[0]-ms2[0]) m1 = list(map(int, input().split())) m2 = list(map(int, input().split())) m3 = list(map(int, input().split())) notMagic = [m1, m2, m3] costs = [] cost = 0 for row in range(3): for col in range(3): cost += abs(notMagic[row][col] - ms1[row][col]) costs.append(cost) cost = 0 for row in range(3): for col in range(3): cost += abs(notMagic[row][col] - ms2[row][col]) costs.append(cost) cost = 0 for row in range(3): for col in range(3): cost += abs(notMagic[row][col] - ms3[row][col]) costs.append(cost) cost = 0 for row in range(3): for col in range(3): cost += abs(notMagic[row][col] - ms4[row][col]) costs.append(cost) cost = 0 for row in range(3): for col in range(3): cost += abs(notMagic[row][col] - ms5[row][col]) costs.append(cost) cost = 0 for row in range(3): for col in range(3): cost += abs(notMagic[row][col] - ms6[row][col]) costs.append(cost) cost = 0 for row in range(3): for col in range(3): cost += abs(notMagic[row][col] - ms7[row][col]) costs.append(cost) cost = 0 for row in range(3): for col in range(3): cost += abs(notMagic[row][col] - ms8[row][col]) costs.append(cost) print(min(costs))