import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int mat[][] = new int[3][3]; for(int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ mat[i][j] = scan.nextInt(); } } int[][] magic1 = {{4, 9, 2}, {3 ,5, 7}, {8, 1, 6}}; int[][] magic2 = {{2, 7, 6}, {9 ,5, 1}, {4, 3, 8}}; int[][] magic3 = {{6, 1, 8}, {7 ,5, 3}, {2, 9, 4}}; int[][] magic4 = {{8, 3, 4}, {1 ,5, 9}, {6, 7, 2}}; int[][] magic5 = {{8, 1, 6}, {3 ,5, 7}, {4, 9, 2}}; int[][] magic6 = {{2, 9, 4}, {7 ,5, 3}, {6, 1, 8}}; int[][] magic7 = {{4, 3, 8}, {9 ,5, 1}, {2, 7, 6}}; int[][] magic8 = {{6, 7, 2}, {1 ,5, 9}, {8, 3, 4}}; int dif1 = howDif(mat, magic1); int dif2 = howDif(mat, magic2); int dif3 = howDif(mat, magic3); int dif4 = howDif(mat, magic4); int dif5 = howDif(mat, magic5); int dif6 = howDif(mat, magic6); int dif7 = howDif(mat, magic7); int dif8 = howDif(mat, magic8); int leastDif = dif1; if(dif2 < leastDif) leastDif = dif2; if(dif3 < leastDif) leastDif = dif3; if(dif4 < leastDif) leastDif = dif4; if(dif5 < leastDif) leastDif = dif5; if(dif6 < leastDif) leastDif = dif6; if(dif7 < leastDif) leastDif = dif7; if(dif8 < leastDif) leastDif = dif8; System.out.println(leastDif); } static int howDif(int mat[][], int magic[][]) { int dif = 0; for (int i = 0; i < 3; i ++){ for(int j = 0; j < 3; j++){ dif += Math.abs(mat[i][j] - magic[i][j]); } } return dif; } }