#include #include #include #include #include using namespace std; int main() { int a[3][3] = { { 8, 1, 6 }, { 3, 5, 7 }, { 4, 9, 2 } }; int b[3][3] = { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 } }; int c[3][3] = { { 6, 1, 8 }, { 7, 5, 3 }, { 2, 9, 4 } }; int d[3][3] = { { 2, 9, 4 }, { 7, 5, 3 }, { 6, 1, 8 } }; int e[3][3] = { { 8, 3, 4 }, { 1, 5, 9 }, { 6, 7, 2 } }; int f[3][3] = { { 6, 7, 2 }, { 1, 5, 9 }, { 8, 3, 4 } }; int g[3][3] = { { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 } }; int h[3][3] = { { 2, 7, 6 }, { 9, 5, 1 }, { 4, 3, 8 } }; int s[3][3]; int cost[8]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> s[i][j]; } } int count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (s[i][j] > a[i][j]) count += (s[i][j] - a[i][j]); else count += (a[i][j] - s[i][j]); } } cost[0] = count; count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (s[i][j] > b[i][j]) count += (s[i][j] - b[i][j]); else count += (b[i][j] - s[i][j]); } } cost[1] = count; count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (s[i][j] > c[i][j]) count += (s[i][j] - c[i][j]); else count += (c[i][j] - s[i][j]); } } cost[2] = count; count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (s[i][j] > d[i][j]) count += (s[i][j] - d[i][j]); else count += (d[i][j] - s[i][j]); } } cost[3] = count; count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (s[i][j] > e[i][j]) count += (s[i][j] - e[i][j]); else count += (e[i][j] - s[i][j]); } } cost[4] = count; count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (s[i][j] > f[i][j]) count += (s[i][j] - f[i][j]); else count += (f[i][j] - s[i][j]); } } cost[5] = count; count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (s[i][j] > g[i][j]) count += (s[i][j] - g[i][j]); else count += (g[i][j] - s[i][j]); } } cost[6] = count; count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (s[i][j] > h[i][j]) count += (s[i][j] - h[i][j]); else count += (h[i][j] - s[i][j]); } } cost[7] = count; int smallest = cost[0]; for (int i = 0; i < 8; i++) { if (cost[i] < smallest) smallest = cost[i]; } cout << smallest; return 0; }