import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; class compute{ int[] sum={0,0,0,0,0,0,0,0}; int x=0; void calculate(int[][] a,int[][] b){ int temp=0; int i,j,k; for(i=0;i<3;i++) for(j=0;j<3;j++){ if(a[i][j]!=b[i][j]){ //continue; //else{ if(a[i][j]>b[i][j]) temp+=a[i][j]-b[i][j]; else temp+=b[i][j]-a[i][j]; } } sum[x++]=temp; } int calculateMin(){ int min; int i; min=sum[0]; for(i=1;i<8;i++){ if(min>sum[i]) min=sum[i]; } //for(i=0;i<8;i++) // { // System.out.println(sum[i]); // } return min; } } public class Solution { /* void calculate(int[][] a,int[][] b){ int i,j,k; for(i=0;i<3;i++) for(j=0;j<3;j++){ if(a[i][j]==b[i][j]) continue; else{ if(a[i][j]>b[i][j]) temp=a[i][j]-b[i][j]; else temp=b[i][j]-a[i][j]; } } sum[x++]=temp; } */ public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ compute ob = new compute(); int[][] arr1 = {{4,9,2},{3,5,7},{8,1,6}} ; int[][] arr2 = {{4,3,8},{9,5,1,},{2,7,6}}; int[][] arr3 = {{8,1,6},{3,5,7},{4,9,2}}; int[][] arr4 = {{8,3,4},{1,5,9},{6,7,2}}; int[][] arr5 = {{6,1,8},{7,5,3},{2,9,4}}; int[][] arr6 = {{2,9,4},{7,5,3},{6,1,8}}; int[][] arr7 = {{6,7,2},{1,5,9},{8,3,4}}; int[][] arr8 = {{2,7,6},{9,5,1},{4,3,8}}; int[] sum=new int[8]; int max,i,j; //int[][] dummy={{1,2,3},{4,5,6},{7,8,9}}; int [][] array = new int[3][3]; int ans; Scanner s = new Scanner (System.in); for(i=0;i<3;i++) for(j=0;j<3;j++) array[i][j] = s.nextInt(); ob.calculate(array,arr1); ob.calculate(array,arr2); ob.calculate(array,arr3); ob.calculate(array,arr4); ob.calculate(array,arr5); ob.calculate(array,arr6); ob.calculate(array,arr7); ob.calculate(array,arr8); ans = ob.calculateMin(); System.out.println(ans); } }