import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[][]matrix = new int[3][3]; int costo = 0; for(int i=0; i<3;i++){ for(int j=0; j<3;j++){ matrix[i][j] = in.nextInt(); } } if(esMagico(matrix)){ System.out.println(costo); }else{ System.out.println(solucion(matrix)); } } public static boolean esMagico(int[][] matrix){ boolean valor = true; int suma = 0; miLoop: for(int i=0; i<3;i++){ for(int j=0; j<3;j++){ suma += matrix[i][j]; } if(suma == 15) suma = 0; else{ valor = false; break miLoop; } } if(valor){ int diagonal1 = matrix[0][0] + matrix[1][1] + matrix[2][2]; int diagonal2 = matrix[0][2] + matrix[1][1] + matrix[2][0]; if(diagonal1 == 15 && diagonal2 == 15){ valor = true; }else{ valor = false; } } return valor; } public static int costo(int a, int b){ return (a-b) >= 0? (a-b): (a-b)*-1; } public static int calCosto(int[][] matrix, int[][] solucion){ int costo = 0; miLoop: for(int i=0; i<3;i++){ for(int j=0; j<3;j++){ costo += costo(matrix[i][j], solucion[i][j]); } } return costo; } public static int solucion(int[][] matrix){ int[][] sol1 = {{4,9,2}, {3, 5, 7}, {8,1,6}}; int[][] sol2 = {{8,3,4}, {1,5,9}, {6,7,2}}; int[][] sol3 = {{6,1,8}, {7,5,3}, {2,9,4}}; int[][] sol4 = {{2,7,6}, {9,5,1}, {4,3,8}}; int[][] sol5 = {{8,1,6}, {3,5,7}, {4,9,2}}; int[][] sol6 = {{6,7,2}, {1,5,9}, {8,3,4}}; int[][] sol7 = {{2,9,4}, {7,5,3}, {6,1,8}}; int[][] sol8 = {{4,3,8}, {9,5,1}, {2,7,6}}; int a = calCosto(matrix, sol1); int b = calCosto(matrix, sol2); int c = calCosto(matrix, sol3); int d = calCosto(matrix, sol4); int e = calCosto(matrix, sol5); int f = calCosto(matrix, sol6); int g = calCosto(matrix, sol7); int h = calCosto(matrix, sol8); int min = 10000000; if (a