#include #include #include #include #include using namespace std; int diff(const vector>& a, const vector>& b) { int sum = 0; for(int i=0;i<3;i++) { for(int j=0; j<3; j++) sum += abs(a[i][j] - b[i][j]); } return sum; } int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ vector> a(3, vector(3)); for(int i=0; i<3; i++) for(int j=0; j<3; j++) cin >> a[i][j]; vector>> b { { {4,9,2}, {3,5,7}, {8,1,6} }, // orig { {2,9,4}, {7,5,3}, {6,1,8} }, // flip horiz { {8,1,6}, {3,5,7}, {4,9,2} }, // flip vert { {6,1,8}, {7,5,3}, {2,9,4} }, // flip both { {8,3,4}, {1,5,9}, {6,7,2} }, // rot 90 degree { {4,3,8}, {9,5,1}, {2,7,6} }, // flip horiz { {6,7,2}, {1,5,9}, {8,3,4} }, // flip vert { {2,7,6}, {9,5,1}, {4,3,8} } // flip both }; int r = 0x7fffffff; for(const auto& p : b) r = min(r, diff(a, p)); cout << r << endl; return 0; }