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 s = new Scanner(System.in); int i,j; int arr[][] = new int[3][3]; for(i=0;i<3;i++) { for(j=0;j<3;j++) { arr[i][j] = s.nextInt(); } } int x1[][] = { {4,9,2}, {3,5,7}, {8,1,6} }; int x2[][] = { {4,3,8}, {9,5,1}, {2,7,6} }; int x3[][] = { {2,7,6}, {9,5,1}, {4,3,8} }; int x4[][] = { {2,9,4}, {7,5,3}, {6,1,8} }; int x5[][] = { {8,1,6}, {3,5,7}, {4,9,2} }; int x6[][] = { {8,3,4}, {1,5,9}, {6,7,2} }; int x7[][] = { {6,1,8}, {7,5,3}, {2,9,4} }; int x8[][] = { {6,7,2}, {1,5,9}, {8,3,4} }; int sub1[][] = new int[3][3]; int sub2[][] = new int[3][3]; int sub3[][] = new int[3][3]; int sub4[][] = new int[3][3]; int sub5[][] = new int[3][3]; int sub6[][] = new int[3][3]; int sub7[][] = new int[3][3]; int sub8[][] = new int[3][3]; int sum1=0,sum2=0,sum3=0,sum4=0,sum5=0,sum6=0,sum7=0,sum8=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { sub1[i][j]=Math.abs(x1[i][j]-arr[i][j]); sum1+=sub1[i][j]; sub2[i][j]=Math.abs(x2[i][j]-arr[i][j]); sum2+=sub2[i][j]; sub3[i][j]=Math.abs(x3[i][j]-arr[i][j]); sum3+=sub3[i][j]; sub4[i][j]=Math.abs(x4[i][j]-arr[i][j]); sum4+=sub4[i][j]; sub5[i][j]=Math.abs(x5[i][j]-arr[i][j]); sum5+=sub5[i][j]; sub6[i][j]=Math.abs(x6[i][j]-arr[i][j]); sum6+=sub6[i][j]; sub7[i][j]=Math.abs(x7[i][j]-arr[i][j]); sum7+=sub7[i][j]; sub8[i][j]=Math.abs(x8[i][j]-arr[i][j]); sum8+=sub8[i][j]; } } int p1 = Math.min(sum1,sum2); int p2 = Math.min(sum3, sum4); int p3 = Math.min(sum5, sum6); int p4 = Math.min(sum7, sum8); int q1 = Math.min(p1, p2); int q2 = Math.min(p3,p4); int q = Math.min(q1, q2); System.out.println(q); } }