#include #include #include #include #include using namespace std; int Cost(vector &a, vector &b) { int cost=0; for (int i=0; i<9; i++) cost += abs(a[i] - b[i]) ; return cost; } int min(int a, int b) { return (a > solutions = { { 4, 9, 2, 3, 5, 7, 8, 1, 6 }, { 2, 9, 4, 7, 5, 3, 6, 1, 8 }, // reflect horiz { 8, 1, 6, 3, 5, 7, 4, 9, 2 }, // reflect vert { 6, 1, 8, 7, 5, 3, 2, 9, 4 }, // reflect horiz+vert { 8, 3, 4, 1, 5, 9, 6, 7, 2 }, // rotate { 4, 3, 8, 9, 5, 1, 2, 7, 6 }, // reflect horiz { 6, 7, 2, 1, 5, 9, 8, 3, 4 }, // reflect vert { 2, 7, 6, 9, 5, 1, 4, 3, 8 } // reflect horiz+vert }; int main() { vector square; for (int i =0; i<9; i++) { int x; cin >> x; square.push_back(x); } int mincost = 9*9; for (auto soln: solutions) mincost = min(Cost(soln,square), mincost); cout << mincost << endl; return 0; }