#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
long long a[3][3];
int b[3][3] = { {8,1,6},
                                   {3,5,7},
                                   {4,9,2}  };
int c[3][3] = { {4,3,8},
                                   {9,5,1},
                                   {2,7,6}  };
int d[3][3] = { {2,9,4},
                                   {7,5,3},
                                   {6,1,8}  };
int e[3][3] = { {6,7,2},
                                   {1,5,9},
                                   {8,3,4}  };
int f[3][3] = { {6,1,8},
                                   {7,5,3},
                                   {2,9,4}  };
int g[3][3] = { {8,3,4},
                                   {1,5,9},
                                   {6,7,2}  };
int h[3][3] = { {4,9,2},
                                   {3,5,7},
                                   {8,1,6}  };
int k[3][3] = { {2,7,6},
                                   {9,5,1},
                                   {4,3,8}  };
priority_queue <long long> pq;
priority_queue <long long> po;
long long visited[10];
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    long long i,j;
    long long sb,sc,sd,se,sf,sg,sh,sk;
    sb=0;
    sc=0;
    sd=0;
    se=0;
    sf=0;
    sg=0;
    sh=0;
    sk=0;
    for(i=1;i<=9;i++)
        {
        visited[i]=0;
    }
    for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
                {
                    cin>>a[i][j];
                    sb+=abs(a[i][j]-b[i][j]);
                sc+=abs(a[i][j]-c[i][j]);
                sd+=abs(a[i][j]-c[i][j]);
                se+=abs(a[i][j]-e[i][j]);
                sf+=abs(a[i][j]-f[i][j]);
                sg+=abs(a[i][j]-g[i][j]);
                sh+=abs(a[i][j]-h[i][j]);
                sk+=abs(a[i][j]-k[i][j]);
                
                }
        }
    pq.push((-1)*sb);
     pq.push((-1)*sc); pq.push((-1)*sd); pq.push((-1)*se);
     pq.push((-1)*sf);
     pq.push((-1)*sg);
     pq.push((-1)*sh);
     pq.push((-1)*sk);
    cout<<(-1)*pq.top();
   
    
    return 0;
}