square = [map(int, raw_input().split())] for i in range(0, 2): square.append(map(int, raw_input().split())) ms1 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]] ms2 = [[4, 3, 8], [9, 5, 1], [2, 7, 6]] ms3 = [[2, 9, 4], [7, 5, 3], [6, 1, 8]] ms4 = [[6, 7, 2], [1, 5, 9], [8, 3, 4]] ms5 = [[6, 1, 8], [7, 5, 3], [2, 9, 4]] ms6 = [[8, 3, 4], [1, 5, 9], [6, 7, 2]] ms7 = [[4, 9, 2], [3, 5, 7], [8, 1, 6]] ms8 = [[2, 7, 6], [9, 5, 1], [4, 3, 8]] all_ms = [ms1, ms2, ms3, ms4, ms5, ms6, ms7, ms8] min_cost = float("inf") for ms in all_ms: ms_cost = 0 for i in range(0, 3): for j in range(0, 3): ms_cost += abs(square[i][j] - ms[i][j]) if ms_cost < min_cost: min_cost = ms_cost print(min_cost)