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) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner in = new Scanner(System.in); int[][] matrix = new int[3][3]; for(int row=0;row<3;row++) { for(int col=0;col<3;col++) { matrix[row][col] = in.nextInt(); } } int[] result = new int[8]; /*Initialize four matrix */ int[][] m0 = {{8,1,6},{3,5,7},{4,9,2}}; int[][] m1 = {{8,3,4},{1,5,9},{6,7,2}}; int[][] m2 = {{4,3,8},{9,5,1},{2,7,6}}; int[][] m3 = {{4,9,2},{3,5,7},{8,1,6}}; for(int row=0;row<3;row++) { for(int col=0;col<3;col++) { if(matrix[row][col] != m0[row][col]) { result[0] = result[0] + Math.abs(matrix[row][col] - m0[row][col]); } if(matrix[row][col] != Math.abs(10 - m0[row][col])) { result[1] = result[1] + Math.abs(matrix[row][col] - Math.abs(10 - m0[row][col])); } if(matrix[row][col] != m1[row][col]) { result[2] = result[2] + Math.abs(matrix[row][col] - m1[row][col]); } if(matrix[row][col] != Math.abs(10 - m1[row][col])) { result[3] = result[3] + Math.abs(matrix[row][col] - Math.abs(10 - m1[row][col])); } if(matrix[row][col] != m2[row][col]) { result[4] = result[4] + Math.abs(matrix[row][col] - m2[row][col]); } if(matrix[row][col] != Math.abs(10 - m2[row][col])) { result[5] = result[5] + Math.abs(matrix[row][col] - Math.abs(10 - m2[row][col])); } if(matrix[row][col] != m3[row][col]) { result[6] = result[6] + Math.abs(matrix[row][col] - m3[row][col]); } if(matrix[row][col] != Math.abs(10 - m3[row][col])) { result[7] = result[7] + Math.abs(matrix[row][col] - Math.abs(10 - m3[row][col])); } } } int min = 1000; for(int i=0;i<8;i++) { if(result[i] < min) { min = result[i]; } } System.out.println(min); } }