#include using namespace std; typedef long long LL; typedef double dbl; #define all(x) (x).begin(), (x).end() #ifdef _DEBUG # define LOG(x...) fprintf(stderr, x) #else # define LOG(x...) 0 #endif const int INF = 1000000000; const int MOD = 1000000007; const dbl EPS = 1e-9; int main() { #ifdef _DEBUG freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin.tie(0); cin.sync_with_stdio(0); int s[3][3]; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) cin >> s[i][j]; int cost = INF; vector p(9); for (int i = 0; i < 9; ++i) p[i] = i + 1; do { bool ok = 1; int c = p[0] + p[1] + p[2]; for (int i = 0; i < 3; ++i) ok &= c == p[3 * i] + p[3 * i + 1] + p[3 * i + 2]; for (int i = 0; i < 3; ++i) ok &= c == p[i] + p[3 + i] + p[6 + i]; ok &= c == p[0] + p[4] + p[8]; ok &= c == p[2] + p[4] + p[6]; if (ok) { int cur = 0; for (int i = 0; i < 9; ++i) cur += abs(p[i] - s[i / 3][i % 3]); cost = min(cost, cur); } } while (next_permutation(all(p))); cout << cost << endl; return 0; }