import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class SolutionMagicSquare {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        int[][] arr = new int[3][3];
        int cost = 0;
        
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                arr[i][j] = in.nextInt();
            }
        }
        
        int finalCost = calcCost(arr);
        int temp = 0;
        
        arr = rotateSquare(arr);
        temp = calcCost(arr);
        if(temp < finalCost)
        {
            finalCost = temp;
        }
        
        arr = rotateSquare(arr);
        temp = calcCost(arr);
        if(temp < finalCost)
        {
            finalCost = temp;
        }
        
        arr = rotateSquare(arr);
        temp = calcCost(arr);
        if(temp < finalCost)
        {
            finalCost = temp;
        }
        
        System.out.println(finalCost);
    }
    
    public static int calcCost(int[][] arr)
    {
        int cost = 0;
        cost+=Math.abs(arr[0][0] - 4);
        cost+=Math.abs(arr[0][1] - 9);
        cost+=Math.abs(arr[0][2] - 2);
        cost+=Math.abs(arr[1][0] - 3);
        cost+=Math.abs(arr[1][1] - 5);
        cost+=Math.abs(arr[1][2] - 7);
        cost+=Math.abs(arr[2][0] - 8);
        cost+=Math.abs(arr[2][1] - 1);
        cost+=Math.abs(arr[2][2] - 6);
        
        int ocost = 0;
        
        ocost+=Math.abs(arr[0][0] - 2);
        ocost+=Math.abs(arr[0][1] - 9);
        ocost+=Math.abs(arr[0][2] - 4);
        ocost+=Math.abs(arr[1][0] - 7);
        ocost+=Math.abs(arr[1][1] - 5);
        ocost+=Math.abs(arr[1][2] - 3);
        ocost+=Math.abs(arr[2][0] - 6);
        ocost+=Math.abs(arr[2][1] - 1);
        ocost+=Math.abs(arr[2][2] - 8);
        
        return cost < ocost ? cost : ocost;
    }
    
    public static int[][] rotateSquare(int[][] arr)
    {
        int[][] lastArray = new int[3][3];
        
        lastArray[0][0] = arr[2][0];
        lastArray[0][1] = arr[1][0];
        lastArray[0][2] = arr[0][0];
        lastArray[1][0] = arr[2][1];
        lastArray[1][1] = arr[1][1];
        lastArray[1][2] = arr[0][1];
        lastArray[2][0] = arr[2][2];
        lastArray[2][1] = arr[1][2];
        lastArray[2][2] = arr[0][2];
        
        return lastArray;
    }
}