#include #include #include #include #include #include using namespace std; bool check_vector(vector& vec) { bool r1 = vec[0] + vec[1] + vec[2] == 15; bool r2 = vec[3] + vec[4] + vec[5] == 15; bool r3 = vec[6] + vec[7] + vec[8] == 15; bool h1 = vec[0] + vec[3] + vec[6] == 15; bool h2 = vec[1] + vec[4] + vec[7] == 15; bool h3 = vec[2] + vec[5] + vec[8] == 15; bool v1 = vec[2] + vec[4] + vec[6] == 15; bool v2 = vec[0] + vec[4] + vec[8] == 15; return r1 && r2 && r3 && h1 && h2 && h3 && v1 && v2; } int main() { vector vec(9); int cost = 0; for (int i = 0; i < 9; ++i) { cin >> vec[i]; } if (check_vector(vec)) { cout << cost; return 0; } vector> vec_sol; vector b_sol{1, 3, -1, -3}; vector a_sol{1, 3, -1, -3}; int count = 0; for (auto& b : b_sol) { for (auto& a : a_sol) { if (abs(a) == abs(b)) continue; int c = 5; vec_sol.push_back(vector()); vec_sol[count].push_back(c - b); vec_sol[count].push_back(c + (a + b)); vec_sol[count].push_back(c - a); vec_sol[count].push_back(c - (a - b)); vec_sol[count].push_back(c); vec_sol[count].push_back(c + (a - b)); vec_sol[count].push_back(c + a); vec_sol[count].push_back(c - (a + b)); vec_sol[count].push_back(c + b); count++; } } cost = numeric_limits::max(); for (int vec_i = 0; vec_i < vec_sol.size(); ++vec_i) { int sol_cost = 0; for (int i = 0; i < 9; ++i) { sol_cost += abs(vec[i] - vec_sol[vec_i][i]); } cost = min(cost, sol_cost); } cout << cost; return 0; }