#include #include #include #include #include using namespace std; const int ms[3][3] = {{8, 1, 6}, {3, 5, 7}, {4, 9, 2}}; int s[3][3]; inline void conv(int x, int y, int &x0, int &y0, int di) { switch (di & 3) { case 0: x0 = x, y0 = y; break; case 1: x0 = 2 - y, y0 = x; break; case 2: x0 = 2 - x, y0 = 2 - y; break; case 3: x0 = y, y0 = 2 - x; break; } if (di & 4) x0 = 2 - x0; } void printm(const int (&s)[3][3], int di) { int x0, y0; for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { conv(x, y, x0, y0, di); printf("%d", s[y0][x0]); } putchar('\n'); } } int diff(const int (&s)[3][3], int di) { int sum = 0, x0, y0; for (int y = 0; y < 3; y++) for (int x = 0; x < 3; x++) { conv(x, y, x0, y0, di); sum += abs(s[y][x] - ms[y0][x0]); } return sum; } int main() { for (int y = 0; y < 3; y++) for (int x = 0; x < 3; x++) cin >> s[y][x]; int mind = 1 << 30; for (int di = 0; di < 8; di++) { // printm(ms, di); int d = diff(s, di); if (mind > d) mind = d; } printf("%d\n", mind); return 0; }