#include #include #include #include #include using namespace std; const vector m = {2, 9, 4, 7, 5, 3, 6, 1, 8}; vector rotateCW(vector a) { vector ret = {a[6], a[3], a[0], a[7], a[4], a[1], a[8], a[5], a[2]}; return ret; } vector flipV(vector a) { vector ret = {a[6], a[7], a[8], a[3], a[4], a[5], a[0], a[1], a[2]}; return ret; } int cmp(vector a) { int ret = 0; for (int i = 0; i < 9; i++) { ret += abs(m[i] - a[i]); } return ret; } int tst(vector a) { return min(cmp(a), min(cmp(rotateCW(a)), min(cmp(rotateCW(rotateCW(a))), cmp(rotateCW(rotateCW(rotateCW(a))))))); } int main() { vector square(9); for (int i = 0; i < 9; i++) { int l; cin >> l; square[i] = l; } int a = tst(square); int b = tst(flipV(square)); cout << min(a, b) << endl; return 0; }