using System;
using System.Collections.Generic;
using System.IO;
class Solution {
    static void Main(String[] args) {
        int[,,] options = new int[,,]{
            {{8, 1, 6},{3, 5, 7},{4, 9, 2}},
            {{4, 3, 8},{9, 5, 1},{2, 7, 6}},    
            {{2, 9, 4},{7, 5, 3},{6, 1, 8}},
            {{6, 7, 2},{1, 5, 9},{8, 3, 4}},
            {{6, 1, 8},{7, 5, 3},{2, 9, 4}},
            {{8, 3, 4},{1, 5, 9},{6, 7, 2}},
            {{4, 9, 2},{3, 5, 7},{8, 1, 6}},
            {{2, 7, 6},{9, 5, 1},{4, 3, 8}}
        };
        
        List<List<int>> inputSquare = new List<List<int>>();
        for(int i = 0; i < 3; ++i)
        {
            inputSquare.Add(new List<int>(Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse)));
        }
        
        int minChanges = Int32.MaxValue;
        
        for(int o = 0; o < options.GetLength(0); ++o)
        {
            int changeCount = 0;
            for(int x = 0; x < 3; ++x)
            {
                for(int y = 0; y < 3; ++y)
                {
                    changeCount += Math.Abs(options[o,y,x] - inputSquare[y][x]);
                }
            }
            
            minChanges = Math.Min(minChanges, changeCount);
        }
        
        Console.WriteLine(minChanges);
    }
}