#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int abs(int a) {
    if(a < 0){ 
        a = a * -1;
    }
    return a;
}

int calcCost(int arr[3][3], int base[3][3]) {
    int totCost = 0;
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 3; j++) {
            totCost += abs(arr[i][j] - base[i][j]);
        }
    }
    return totCost;
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int sq[3][3];
    int sum = 0;

    int one[3][3] = {
        {8, 1, 6},
        {3, 5, 7},
        {4, 9, 2}
    };
    int two[3][3] = {
        {6, 1, 8},
        {7, 5, 3},
        {2, 9, 4}
    };
    int three[3][3] = {
        {4, 9, 2},
        {3, 5, 7},
        {8, 1, 6}
    };
    int four[3][3] = {
        {2, 9, 4},
        {7, 5, 3},
        {6, 1, 8}
    };
    int five[3][3] = {
        {8, 3, 4},
        {1, 5, 9},
        {6, 7, 2}
    };
    int six[3][3] = {
        {4, 3, 8},
        {9, 5, 1},
        {2, 7, 6}
    };
    int seven[3][3] = {
        {6, 7, 2},
        {1, 5, 9},
        {8, 3, 4}
    };
    int eight[3][3] = {
        {2, 7, 6},
        {9, 5, 1},
        {4, 3, 8}
    };
   
    
    
    int row[3];
    int col[3];
    for(int i = 0; i < 3; i++) {
        int rowSum = 0;
        for(int j = 0; j < 3; j++) {
            cin >> sq[i][j];
            sum+=sq[i][j];
            //rowSum += sq[i][j];
        }
        /*
        if(rowSum > 15) {
            row[i] = rowSum - 15;
        } else if (rowSum < 15) {
            row[i] = rowSum - 15;
        } else {
            row[i] = 0;
        }
        */
    }
    /*
    for(int j = 0; j < 3; j++) {
        int colSum = 0;
        for(int i = 0; i < 3; i++) {
            
            colSum += sq[i][j];
            
        }
        if(colSum > 15) {
            col[j] = colSum - 15;
        } else if (colSum < 15) {
            col[j] = colSum - 15;
        } else {
            col[j] = 0;
        }
    }
    */
    int costs[8] = { 
        calcCost(sq, one),
        calcCost(sq, two),
        calcCost(sq, three),
        calcCost(sq, four),
        calcCost(sq, five),
        calcCost(sq, six),
        calcCost(sq, seven),
        calcCost(sq, eight) 
    };
    
    int min = costs[0];
    for(int i = 0; i < 8; i++) {
        if(costs[i] < min) {
            min = costs[i];
        }
    }
    
    cout << min;
    return 0;
}