import java.io.*; import java.util.*; import java.math.*; public class Solution { static int[][] s1 = {{8,1,6}, {3,5,7}, {4,9,2}}; static int[][] s2 = {{4,3,8}, {9,5,1}, {2,7,6}}; static int[][] s3 = {{2,9,4}, {7,5,3}, {6,1,8}}; static int[][] s4 = {{6,7,2}, {1,5,9}, {8,3,4}}; static int[][] s5 = {{6,1,8}, {7,5,3}, {2,9,4}}; static int[][] s6 = {{8,3,4}, {1,5,9}, {6,7,2}}; static int[][] s7 = {{4,9,2}, {3,5,7}, {8,1,6}}; static int[][] s8 = {{2,7,6}, {9,5,1}, {4,3,8}}; static int[][][] magicSquares = {s1,s2,s3,s4,s5,s6,s7,s8}; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[][] matrix = new int[3][3]; for(int x = 0; x < 3; x++) { for(int y = 0; y < 3; y++) { matrix[x][y] = scan.nextInt(); } } int cost = Arrays.asList(magicSquares).stream().mapToInt(s -> distance(matrix, s)).min().getAsInt(); System.out.println(cost); } static int distance(int[][] first, int[][] second) { int distance = 0; for(int x = 0; x < 3; x++) { for(int y = 0; y < 3; y++) { distance += Math.abs(first[x][y] - second[x][y]); } } return distance; } }