import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) throws Exception {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        
        List<int[]> magic_3by3_squares = new ArrayList<int[]>();
        magic_3by3_squares.add(new int[] {4, 9, 2, 3, 5, 7, 8, 1, 6});
        magic_3by3_squares.add(new int[] {8, 1, 6, 3, 5, 7, 4, 9, 2});
        
        magic_3by3_squares.add(new int[] {6, 7, 2, 1, 5, 9, 8, 3, 4});
        magic_3by3_squares.add(new int[] {8, 3, 4, 1, 5, 9, 6, 7, 2});
        
        magic_3by3_squares.add(new int[] {6, 1, 8, 7, 5, 3, 2, 9, 4});
        magic_3by3_squares.add(new int[] {2, 9, 4, 7, 5, 3, 6, 1, 8});
        
        magic_3by3_squares.add(new int[] {4, 3, 8, 9, 5, 1, 2, 7, 6});
        magic_3by3_squares.add(new int[] {2, 7, 6, 9, 5, 1, 4, 3, 8});
        
        Set<Integer> costs = new TreeSet<Integer>();
        
        int array[] = new int[9];
        int index = 0;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for(int j = 0; j < 3; ++j) {
            String input = in.readLine();
            String is[] = input.split(" ");      
            for(String i : is)
                array[index++] = Integer.parseInt(i);
        }
        
        for(int[] magic_3by3_square : magic_3by3_squares) {
            costs.add(help(array, magic_3by3_square));
        }
        
        for(Integer cost : costs) {
            System.out.println(cost);
            break;
        }
    }
    
    private static int help(int array[], int ms[]) {
        int res = 0;
        for(int i = 0; i < 9; ++i)
            res += Math.abs(ms[i] - array[i]);
        
        return res;
    }
}