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. */ int[][] ms1 = new int[3][3]; int[][] ms2 = new int[3][3]; int[][] ms3 = new int[3][3]; int[][] ms4 = new int[3][3]; int[][] ms5 = new int[3][3]; int[][] ms6 = new int[3][3]; int[][] ms7 = new int[3][3]; int[][] ms8 = new int[3][3]; int[][] ms9 = new int[3][3]; ms1[0][0] = 4; ms1[0][1] = 9; ms1[0][2] = 2; ms1[1][0] = 3; ms1[1][1] = 5; ms1[1][2] = 7; ms1[2][0] = 8; ms1[2][1] = 1; ms1[2][2] = 6; ms2[0][0] = 2; ms2[0][1] = 7; ms2[0][2] = 6; ms2[1][0] = 9; ms2[1][1] = 5; ms2[1][2] = 1; ms2[2][0] = 4; ms2[2][1] = 3; ms2[2][2] = 8; ms3[0][0] = 6; ms3[0][1] = 1; ms3[0][2] = 8; ms3[1][0] = 7; ms3[1][1] = 5; ms3[1][2] = 3; ms3[2][0] = 2; ms3[2][1] = 9; ms3[2][2] = 4; ms4[0][0] = 8; ms4[0][1] = 3; ms4[0][2] = 4; ms4[1][0] = 1; ms4[1][1] = 5; ms4[1][2] = 9; ms4[2][0] = 6; ms4[2][1] = 7; ms4[2][2] = 2; ms5[0][0] = 2; ms5[0][1] = 9; ms5[0][2] = 4; ms5[1][0] = 7; ms5[1][1] = 5; ms5[1][2] = 3; ms5[2][0] = 6; ms5[2][1] = 1; ms5[2][2] = 8; ms6[0][0] = 6; ms6[0][1] = 7; ms6[0][2] = 2; ms6[1][0] = 1; ms6[1][1] = 5; ms6[1][2] = 9; ms6[2][0] = 8; ms6[2][1] = 3; ms6[2][2] = 4; ms7[0][0] = 8; ms7[0][1] = 1; ms7[0][2] = 6; ms7[1][0] = 3; ms7[1][1] = 5; ms7[1][2] = 7; ms7[2][0] = 4; ms7[2][1] = 9; ms7[2][2] = 2; ms8[0][0] = 4; ms8[0][1] = 3; ms8[0][2] = 8; ms8[1][0] = 9; ms8[1][1] = 5; ms8[1][2] = 1; ms8[2][0] = 2; ms8[2][1] = 7; ms8[2][2] = 6; Scanner in = new Scanner(System.in); int[][] square = new int[3][3]; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { square[i][j] = in.nextInt(); } } int sum1 = 0; int sum2 = 0; int sum3 = 0; int sum4 = 0; int sum5 = 0; int sum6 = 0; int sum7 = 0; int sum8 = 0; for(int n = 0; n < 3; n++){ for(int m = 0; m < 3; m++){ sum1 += Math.abs(square[n][m] - ms1[n][m]); sum2 += Math.abs(square[n][m] - ms2[n][m]); sum3 += Math.abs(square[n][m] - ms3[n][m]); sum4 += Math.abs(square[n][m] - ms4[n][m]); sum5 += Math.abs(square[n][m] - ms5[n][m]); sum6 += Math.abs(square[n][m] - ms6[n][m]); sum7 += Math.abs(square[n][m] - ms7[n][m]); sum8 += Math.abs(square[n][m] - ms8[n][m]); } } int min = 1000000; int[] arr = {sum1,sum2,sum3,sum4,sum5,sum6,sum7,sum8}; for(int x = 0; x < arr.length;x++){ if(arr[x] < min) min = arr[x]; } System.out.println(min); } }