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 scan = new Scanner(System.in); int[][] matrix = new int[3][3]; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { matrix[i][j] = scan.nextInt(); } } int[][] arry0 = new int[3][3]; arry0[0][0] = 4; arry0[0][1] = 9; arry0[0][2] = 2; arry0[1][0] = 3; arry0[1][1] = 5; arry0[1][2] = 7; arry0[2][0] = 8; arry0[2][1] = 1; arry0[2][2] = 6; int[][] arry1 = new int[3][3]; arry1[0][0] = 8; arry1[0][1] = 3; arry1[0][2] = 4; arry1[1][0] = 1; arry1[1][1] = 5; arry1[1][2] = 9; arry1[2][0] = 6; arry1[2][1] = 7; arry1[2][2] = 2; int[][] arry2 = new int[3][3]; arry2[0][0] = 6; arry2[0][1] = 1; arry2[0][2] = 8; arry2[1][0] = 7; arry2[1][1] = 5; arry2[1][2] = 3; arry2[2][0] = 2; arry2[2][1] = 9; arry2[2][2] = 4; int[][] arry3 = new int[3][3]; arry3[0][0] = 2; arry3[0][1] = 7; arry3[0][2] = 6; arry3[1][0] = 9; arry3[1][1] = 5; arry3[1][2] = 1; arry3[2][0] = 4; arry3[2][1] = 3; arry3[2][2] = 8; int[][] arry4 = new int[3][3]; arry4[0][0] = 2; arry4[0][1] = 9; arry4[0][2] = 4; arry4[1][0] = 7; arry4[1][1] = 5; arry4[1][2] = 3; arry4[2][0] = 6; arry4[2][1] = 1; arry4[2][2] = 8; int[][] arry5 = new int[3][3]; arry5[0][0] = 4; arry5[0][1] = 3; arry5[0][2] = 8; arry5[1][0] = 9; arry5[1][1] = 5; arry5[1][2] = 1; arry5[2][0] = 2; arry5[2][1] = 7; arry5[2][2] = 6; int[][] arry6 = new int[3][3]; arry6[0][0] = 6; arry6[0][1] = 7; arry6[0][2] = 2; arry6[1][0] = 1; arry6[1][1] = 5; arry6[1][2] = 9; arry6[2][0] = 8; arry6[2][1] = 3; arry6[2][2] = 4; int[][] arry7 = new int[3][3]; arry7[0][0] = 8; arry7[0][1] = 1; arry7[0][2] = 6; arry7[1][0] = 3; arry7[1][1] = 5; arry7[1][2] = 7; arry7[2][0] = 4; arry7[2][1] = 9; arry7[2][2] = 2; int minCost = 100000; int cost = 0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { cost += Math.abs(matrix[i][j] - arry0[i][j]); } } if(cost < minCost) { minCost = cost; } cost = 0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { cost += Math.abs(matrix[i][j] - arry1[i][j]); } } if(cost < minCost) { minCost = cost; } cost = 0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { cost += Math.abs(matrix[i][j] - arry2[i][j]); } } if(cost < minCost) { minCost = cost; } cost = 0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { cost += Math.abs(matrix[i][j] - arry3[i][j]); } } if(cost < minCost) { minCost = cost; } cost = 0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { cost += Math.abs(matrix[i][j] - arry4[i][j]); } } if(cost < minCost) { minCost = cost; } cost = 0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { cost += Math.abs(matrix[i][j] - arry5[i][j]); } } if(cost < minCost) { minCost = cost; } cost = 0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { cost += Math.abs(matrix[i][j] - arry6[i][j]); } } if(cost < minCost) { minCost = cost; } cost = 0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { cost += Math.abs(matrix[i][j] - arry7[i][j]); } } if(cost < minCost) { minCost = cost; } System.out.println(minCost); } }