/* ye mera template hai apna khud likho bc :P */ /* Author : Sarvagya Agarwal */ #include using namespace std; //defines #define openin freopen("input.txt","r",stdin) #define openout freopen("output.txt","w",stdout) #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define ll long long #define int long long #define mod 1000000007 #define rep(i,a,n) for(__typeof(n) i=a;i<=n;++i) #define all(c) (c).begin(),(c).end() #define ff first #define ss second #define pb push_back #define mp make_pair //templates template T expo(T e, T n){T x=1,p=e;while(n){if(n&1)x=x*p;p=p*p;n>>=1;}return x;} template T power(T e, T n, T m){T x=1,p=e;while(n){if(n&1)x=(x*p)%m;p=(p*p)%m;n>>=1;}return x;} template T InverseEuler(T a, T m){return (a==1? 1 : power(a, m-2, m));} template T gcd(T a, T b){while(a%=b^=a^=b^=a); return b;} template T lcm(T a, T b){return (a*(b/gcd(a,b)));} //debug #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template void __f(const char* name, Arg1&& arg1){ cerr << name << " : " << arg1 << std::endl; } template void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #else #define trace(...) #endif // DOBARA OVERFLOW KI GALTI HUI TO TU CHUTIYA HAI BC // update : TU CHUTIYA HAI BC vectorx ,given,mat[100006],all[1000007]; int cnt = 1 ; bool ok(vector&v) { for(int i=0;i<9;i+=3) if(v[i]+v[i+1]+v[i+2]!=15) return false; rep(i,0,2) { if(v[i]+v[i+3]+v[i+6]!=15) return false; } if(v[0]+v[4]+v[8]!=15) return false ; if(v[2] + v[4] + v[6]!=15) return false; return true ; } int f(int i) { vector y = mat[i] ; int res = 0 ; rep(i,0,8) { res += abs(y[i]-given[i]); } return res ; } int32_t main() { fast; rep(i,0,2) { rep(j,0,2) { int x; cin >> x ; given.pb(x) ; } } rep(i,1,9) { x.pb(i) ; } do{ all[cnt++] = x ; }while(next_permutation(all(x))); int sz = 0 ; rep(i,1,cnt-1) { /*for(auto it : all[i]) trace(it);*/ if(ok(all[i])) mat[sz++] = all[i] ; } int res = 1e7 ; rep(i,0,sz-1) { res = min(res,f(i)); } cout << res ; return 0; }