#include using namespace std; int distance(int A[3][3], int B[3][3]) { int ans = 0; for (int k = 0; k < 3; k++) for (int j = 0; j < 3; j++) ans += abs(A[k][j] - B[k][j]); return ans; } int main() { int M1[3][3] = {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}}; int M2[3][3] = {{2, 9, 4}, {7, 5, 3}, {6, 1, 8}}; int M3[3][3] = {{4, 3, 8}, {9, 5, 1}, {2, 7, 6}}; int M4[3][3] = {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}; int M5[3][3] = {{6, 1, 8}, {7, 5, 3}, {2, 9, 4}}; int M6[3][3] = {{6, 7, 2}, {1, 5, 9}, {8, 3, 4}}; int M7[3][3] = {{8, 1, 6}, {3, 5, 7}, {4, 9, 2}}; int M8[3][3] = {{8, 3, 4}, {1, 5, 9}, {6, 7, 2}}; int A[3][3]; for (int k = 0; k < 3; k++) for (int j = 0; j < 3; j++) cin >> A[k][j]; int Min = 1e9; Min = min(Min, distance(A, M1)); Min = min(Min, distance(A, M2)); Min = min(Min, distance(A, M3)); Min = min(Min, distance(A, M4)); Min = min(Min, distance(A, M5)); Min = min(Min, distance(A, M6)); Min = min(Min, distance(A, M7)); Min = min(Min, distance(A, M8)); cout << Min << endl; return 0; }