import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static int[] nums = new int[10]; public static int[][] checks = new int[3][3]; public static int count = 1000; public static boolean perfect = false; public static List caches = new ArrayList<>(); public static int[][] p1 = new int[][] { {8,1,6},{3,5,7},{4,9,2} }; public static int[][] p2 = new int[][] { {4,3,8},{9,5,1},{2,7,6} }; public static int[][] p3 = new int[][] { {2,9,4},{7,5,3},{6,1,8} }; public static int[][] p4 = new int[][] { {6,7,2},{1,5,9},{8,3,4} }; public static int[][] p5 = new int[][] { {6,1,8},{7,5,3},{2,9,4} }; public static int[][] p6 = new int[][] { {8,3,4},{1,5,9},{6,7,2} }; public static int[][] p7 = new int[][] { {4,9,2},{3,5,7},{8,1,6} }; public static int[][] p8 = new int[][] { {2,7,6},{9,5,1},{4,3,8} }; //Rules //5 in center //even numbers in corners public static void main(String[] args) { Scanner in = new Scanner(System.in); int[][] matrix = new int[3][3]; boolean perfect = false; caches.add(p1); caches.add(p2); caches.add(p3); caches.add(p4); caches.add(p5); caches.add(p6); caches.add(p7); caches.add(p8); for(int x = 0; x < 3; x++) { for(int y = 0; y < 3; y++) { matrix[x][y] = in.nextInt(); nums[matrix[x][y]]++; } } for(int p = 0; p < 8; p++) { int[][] perf = caches.get(p); int score = 0; for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix.length; c++) { score += Math.abs(matrix[r][c] - perf[r][c]); } } if(score < count) { count = score; } } System.out.println(count); } public static void perfectCheck(int[][]arr) { int diagtotal = 0; for(int row = 0; row < 3; row++) { int rowtotal = 0; int coltotal = 0; for(int col = 0; col < 3; col++) { rowtotal += arr[row][col]; coltotal += arr[col][row]; } diagtotal += arr[row][row]; System.out.println("Row " + row + " = " + rowtotal); System.out.println("Column " + row + " = " + coltotal); if(rowtotal != 15 || coltotal != 15) { perfect = false; } } printArray(arr); System.out.println("Diagonal = " + diagtotal); if(diagtotal != 15) { perfect = false; } } public static void printArray(int[][] a) { for(int x = 0; x < a.length; x++) { for(int y = 0; y < a.length; y++) { System.out.print(a[x][y] + " "); } System.out.print("\n"); } } }