import java.util.Random; import java.util.Scanner; public class Solution { public static void main(String[]args) { int matrix1[][] = {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}; int matrix2[][] = {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}}; int matrix3[][] = {{6, 1, 8}, {7, 5, 3}, {2, 9, 4}}; int matrix4[][] = {{8, 3, 4}, {1, 5, 9}, {6, 7, 2}}; int matrix5[][] = {{2, 9, 4}, {7, 5, 3}, {6, 1, 8}}; int matrix6[][] = {{6, 7, 2}, {1, 5, 9}, {8, 3, 4}}; int matrix7[][] = {{8, 1, 6}, {3, 5, 7}, {4, 9, 2}}; int matrix8[][] = {{4, 3, 8}, {9, 5, 1}, {2, 7, 6}}; int input[][] = new int[3][3]; Scanner sc = new Scanner(System.in); for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { input[i][j] = sc.nextInt(); } } int cost = 0; int temp = 0; for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { cost = cost + Math.abs(matrix1[i][j] - input[i][j]); } } temp = cost; cost = 0; for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { cost = cost + Math.abs(matrix2[i][j] - input[i][j]); } } if(cost < temp) temp = cost; cost = 0; for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { cost = cost + Math.abs(matrix3[i][j] - input[i][j]); } } if(cost < temp) temp = cost; cost = 0; for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { cost = cost + Math.abs(matrix4[i][j] - input[i][j]); } } if(cost < temp) temp = cost; cost = 0; for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { cost = cost + Math.abs(matrix5[i][j] - input[i][j]); } } if(cost < temp) temp = cost; cost = 0; for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { cost = cost + Math.abs(matrix6[i][j] - input[i][j]); } } if(cost < temp) temp = cost; cost = 0; for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { cost = cost + Math.abs(matrix7[i][j] - input[i][j]); } } if(cost < temp) temp = cost; cost = 0; for(int i = 0;i < 3;i++) { for(int j = 0;j < 3;j++) { cost = cost + Math.abs(matrix8[i][j] - input[i][j]); } } if(cost < temp) temp = cost; cost = 0; System.out.print(temp); } }