#include #include int compareOffset(int sol[], int square[], int size, int offset, int step) { int i, j = offset, cost = 0; for (i = 0; i < size; i++) { cost += abs(sol[i] - square[j]); j += step; if (j >= size) j -= size; if (j < 0) j += size; } return cost; } int main() { int square[8], newCost, center, cost = 100000; int sol[] = { 8, 3, 4, 9, 2, 7, 6, 1 }; for (int i = 0; i < 3; i++) std::cin >> square[i]; std::cin >> square[7]; std::cin >> center; std::cin >> square[3]; for (int i = 6; i > 3; i--) std::cin >> square[i]; for (int i = 0; i <= 6; i += 2) { newCost = compareOffset(sol, square, 8, i, 1); if (newCost < cost) cost = newCost; } for (int i = 0; i <= 6; i += 2) { newCost = compareOffset(sol, square, 8, i, -1); if (newCost < cost) cost = newCost; } cost += abs(5 - center); std::cout << cost; return 0; }