def rot_clockwise(m): new_m = [] for i in range(3): new_m.append(m[i][:]) tmp = new_m[0][0] new_m[0][0] = new_m[2][0] new_m[2][0] = new_m[2][2] new_m[2][2] = new_m[0][2] new_m[0][2] = tmp tmp = new_m[0][1] new_m[0][1] = new_m[1][0] new_m[1][0] = new_m[2][1] new_m[2][1] = new_m[1][2] new_m[1][2] = tmp return new_m def reflect_vert(m): new_m = [] for i in range(3): new_m.append(m[i][:]) tmp = new_m[i][0] new_m[i][0] = new_m[i][2] new_m[i][2] = tmp return new_m def get_cost(m, n): cost = 0 for r in range(3): for c in range(3): cost += abs(m[r][c] - n[r][c]) return cost square = [] for n in range(3): square.append(list(map(lambda x: int(x),input().split()))) lo_shu = [[4, 9, 2], [3, 5, 7], [8, 1, 6]] matrices = [lo_shu] matrices.append(reflect_vert(lo_shu)) for n in range(2, 8, 2): rotated = rot_clockwise(matrices[-2]) reflected = reflect_vert(rotated) matrices.append(rotated) matrices.append(reflected) min_cost = get_cost(matrices[0], square) for n in range(1, 8): cost = get_cost(matrices[n], square) if cost < min_cost: min_cost = cost print(min_cost)