#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; void print(int64_t (&m)[3][3]) { for (uint64_t i = 0; i < 3; ++i) { for (uint64_t j = 0; j < 3; ++j) cerr << m[i][j] << " "; cerr << endl; } cerr << endl; } void rotate(int64_t (&m)[3][3]) { for (uint64_t i = 0; i < 3; ++i) for (uint64_t j = 0; j < 3; ++j) if (j > i) swap(m[i][j], m[j][i]); for (uint64_t i = 0; i < 3; ++i) reverse(&m[i][0], &m[i][3]); } void flipv(int64_t (&m)[3][3]) { for (uint64_t i = 0; i < 3/2; ++i) for (uint64_t j = 0; j < 3; ++j) swap(m[i][j], m[3-1-i][j]); } void fliph(int64_t (&m)[3][3]) { for (uint64_t i = 0; i < 3; ++i) reverse(&m[i][0], &m[i][3]); } int64_t delta(int64_t (&a)[3][3], int64_t (&b)[3][3]) { int64_t res = 0; for (int64_t i = 0; i < 3; ++i) for (int64_t j = 0; j < 3; ++j) res += abs(a[i][j] - b[i][j]); return res; } void runOne() { int64_t q[3][3]; for (int64_t i = 0; i < 3; ++i) for (int64_t j = 0; j < 3; ++j) cin >> q[i][j]; int64_t a = 1, b = 3, c = 5; int64_t m[3][3] = {{c-b, c+(a+b), c-a} ,{c-(a-b), c, c+(a-b)} ,{c+a, c-(a+b), c+b} }; int64_t res = 1e9; for (uint64_t i = 0; i < 4; ++i) { //print(m); res = min(res, delta(m, q)); fliph(m); //print(m); res = min(res, delta(m, q)); fliph(m); flipv(m); //print(m); res = min(res, delta(m, q)); flipv(m); rotate(m); //cerr << endl; } cout << res << endl; } int main(int, char**) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cerr.tie(NULL); size_t t = 1; //cin >> t; while (t--) runOne(); return 0; }