import java.io.*; import java.util.*; public class Solution { static int[][] ms0= { {4, 9, 2}, {3, 5, 7}, {8, 1, 6} }; static int[][] ms1= { {2, 7, 6}, {9, 5, 1}, {4, 3, 8} }; static int[][] ms2= { {6, 1, 8}, {7, 5, 3}, {2, 9, 4} }; static int[][] ms3= { {8, 3, 4}, {1, 5, 9}, {6, 7, 2} }; static int[][] ms4= { {2, 9, 4}, {7, 5, 3}, {6, 1, 8} }; static int[][] ms5= { {6, 7, 2}, {1, 5, 9}, {8, 3, 4} }; static int[][] ms6= { {8, 1, 6}, {3, 5, 7}, {4, 9, 2} }; static int[][] ms7= { {4, 3, 8}, {9, 5, 1}, {2, 7, 6} }; static int getCost(int[][] a, int[][] b){ int cost = 0; for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ cost+=Math.abs(a[i][j]-b[i][j]); } } return cost; } public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ int [][]a = new int[3][3]; Scanner scan = new Scanner(System.in); for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ a[i][j] = scan.nextInt(); } } int min = -1; List list = new ArrayList<>(); list.add(ms0); list.add(ms1); list.add(ms2); list.add(ms3); list.add(ms4); list.add(ms5); list.add(ms6); list.add(ms7); for(int[][] ms:list){ int cost = getCost(ms, a); if(min == -1){ min = cost; }else{ if(min>cost){ min = cost; } } } System.out.println(min); } }