#include #include #include #include #include #include using namespace std; int magic_squares[8][9] = { {2,7,6, 9,5,1, 4,3,8}, {4,9,2, 3,5,7, 8,1,6}, {8,3,4, 1,5,9, 6,7,2}, {6,1,8, 7,5,3, 2,9,4}, {2,9,4, 7,5,3, 6,1,8}, {6,7,2, 1,5,9, 8,3,4}, {8,1,6, 3,5,7, 4,9,2}, {4,3,8, 9,5,1, 2,7,6} }; bool check_magicness(const int m[]) { if(m[4] != 5) return false; // diag if(m[0]+m[4]+m[8] != 15) return false; if(m[2]+m[4]+m[6] != 15) return false; for(int i=0; i<3; ++i){ // rows if(m[i*3+0] + m[i*3+1] + m[i*3+2] != 15) return false; // column if(m[0+i] + m[3+i] + m[6+i] != 15) return false; } return true; } int calculate_cost(const int square[], const int matrix[]) { int cost = 0; for(int i=0; i<9; ++i){ cost += abs(square[i] - matrix[i]); } return cost; } int main() { int matrix[9]; cin >> matrix[0] >> matrix[1] >> matrix[2]; cin >> matrix[3] >> matrix[4] >> matrix[5]; cin >> matrix[6] >> matrix[7] >> matrix[8]; // if original matrix is a magic square, print 0; if(check_magicness(matrix)) { cout<<0; return 0; } int cost, min_cost = 100; for(int i=0; i<8; ++i){ cost = calculate_cost(magic_squares[i], matrix); if(cost < min_cost) min_cost = cost; } cout<