import java.io.*; import java.util.*; import static java.lang.Math.abs; public class Solution { public static void main(String[] args) { int array[][] = new int[3][3]; int M1[][] = {{8,1,6},{3,5,7},{4,9,2}}; int M2[][] = {{4,9,2},{3,5,7},{8,1,6}}; int M3[][] = {{4,3,8},{9,5,1},{2,7,6}}; int M4[][]={{6,1,8},{7,5,3},{2,9,4}}; int M5[][]={{2,9,4,},{7,5,3},{6,1,8}}; int M6[][]= { {2,7,6},{9,5,1},{4,3,8}}; int M7[][]= {{6,7,2},{1,5,9},{8,3,4}}; int M8[][]={{8,3,4},{1,5,9},{6,7,2}}; int cost[] = {0,0,0,0,0,0,0,0}; Scanner sc = new Scanner(System.in); for(int i=0;i<3;i++ ){ for( int j=0;j<3 ;j++){ array[i][j]= sc.nextInt(); } } for(int i=0;i<3;i++ ){ for( int j=0;j<3 ;j++){ if(M1[i][j] != array[i][j]){ cost[0]+=abs(M1[i][j]-array[i][j]); } } } for(int i=0;i<3;i++ ){ for( int j=0;j<3 ;j++){ if(M2[i][j] != array[i][j]){ cost[1]+=abs(M2[i][j]-array[i][j]); } } } for(int i=0;i<3;i++ ){ for( int j=0;j<3 ;j++){ if(M3[i][j] != array[i][j]){ cost[2]+=abs(M3[i][j]-array[i][j]); } } } for(int i=0;i<3;i++ ){ for( int j=0;j<3 ;j++){ if(M4[i][j] != array[i][j]){ cost[3]+=abs(M4[i][j]-array[i][j]); } } } for(int i=0;i<3;i++ ){ for( int j=0;j<3 ;j++){ if(M5[i][j] != array[i][j]){ cost[4]+=abs(M5[i][j]-array[i][j]); } } } for(int i=0;i<3;i++ ){ for( int j=0;j<3 ;j++){ if(M6[i][j] != array[i][j]){ cost[5]+=abs(M6[i][j]-array[i][j]); } } } for(int i=0;i<3;i++ ){ for( int j=0;j<3 ;j++){ if(M7[i][j] != array[i][j]){ cost[6]+=abs(M7[i][j]-array[i][j]); } } } for(int i=0;i<3;i++ ){ for( int j=0;j<3 ;j++){ if(M8[i][j] != array[i][j]){ cost[7]+=abs(M8[i][j]-array[i][j]); } } } Arrays.sort(cost); System.out.println(cost[0]); /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ } }