using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */
        int[] matrix = new int[9];  
        string wolo =  Console.ReadLine()+" " + Console.ReadLine() + " "+ Console.ReadLine();
        for(int i = 0 ; i < 9 ; i++)
        {
           matrix[i] = int.Parse(wolo[i*2].ToString());
         //  Console.WriteLine(matrix[i]);
        }
       //LEIMOS LOS DATOS 
       //SABEMOS QUE SOLO HAY 8 POSIBLES CUADRADOS PERFECTOS 
         int[][] posibilidad = new int[8][];
            posibilidad[0] =  new int[9] { 8, 1, 6, 3, 5, 7, 4, 9, 2 };
            posibilidad[1] = new int[9] {4,3,8,9,5,1,2,7,6 };
            posibilidad[2] = new int[9] { 2,9,4,7,5,3,6,1,8};
            posibilidad[3]= new int[9] { 6,7,2,1,5,9,8,3,4};
            posibilidad[4] = new int[9] { 6,1,8,7,5,3,2,9,4};
            posibilidad[5] = new int[9] { 8,3,4,1,5,9,6,7,2 };
            posibilidad[6] = new int[9] {  4,9,2,3,5,7,8,1,6};
            posibilidad[7] = new int[9] { 2,7,6,9,5,1,4,3,8};
        
        int menor = 100000;
        int costo =9999 ;
          for(int i = 0 ; i < 8 ; i++)
        {
            costo = getCostMatrix(matrix,posibilidad[i])   ;
             // Console.WriteLine(costo);
            if(menor > costo)
            {
                menor  =  costo;
            }
        }
        
        Console.WriteLine(menor.ToString());
    }
    static int getCostMatrix(int[] matrix1 , int[] matrix2)
    {
        int costo = 0;
       for(int i = 0 ; i < 9 ; i++)
        {
         
           costo += Math.Abs((matrix1[i] - (matrix2[i])));
        }
        
        return costo;
    }
   
}