#include #include #include #include #include using namespace std; int main() { int x[3][3], y[3][3], cost, z[10], best; bool ok; best = 100; for (int i = 0; i < 3; i++){ for (int j = 0; j<3; j++){ cin >> x[i][j]; } } for (int a=1; a<10; a++){ for (int b=1; b<10; b++){ for (int c=1; c<10; c++){ y[0][0] = a; y[0][1] = b; y[1][0] = c; y[0][2] = 15 - a - b; if ((y[0][2] < 1) || (y[0][2] > 9)) continue; y[2][0] = 15 - a - c; if ((y[2][0] < 1) || (y[2][0] > 9)) continue; y[1][1] = 15 - y[0][2] - y[2][0]; if ((y[1][1] < 1) || (y[1][1] > 9)) continue; y[2][2] = 15 - y[0][0] - y[1][1]; if ((y[2][2] < 1) || (y[2][2] > 9)) continue; y[1][2] = 15 - y[0][2] - y[2][2]; if ((y[1][2] < 1) || (y[1][2] > 9)) continue; y[2][1] = 15 - y[2][0] - y[2][2]; if ((y[2][1] < 1) || (y[2][1] > 9)) continue; for (int i = 0; i < 10; i++){ z[i] = 0; } ok = true; cost = 0; for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ cost += abs(y[i][j] - x[i][j]); z[y[i][j]] ++; if (z[y[i][j]] > 1) ok = false; } } if (!ok) continue; if (cost < best) best = cost; } } } cout << best; /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }