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 in = new Scanner(System.in); int[][] matrix = new int [3][3]; //store input matrix boolean[] row = new boolean[3]; //check summation of each row. boolean[] column = new boolean[3]; boolean backslash; boolean found = false; boolean forwardslash; boolean duplicate9 = false; int cost = 0; int costA = 0; int costB = 0; int costC = 0; int costD = 0; int costE = 0; int costF = 0; int costG = 0; int costH = 0; int lowestCost = 0; //store input in a 2D Array for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { matrix[i][j] = in.nextInt(); } } //Check if 5 is in the middle if (!(matrix[1][1]==15)) { cost = Math.abs(5-matrix[1][1]); costA = cost; costB = cost; costC = cost; costD = cost; costE = cost; costF = cost; costG = cost; costH = cost; matrix[1][1] = 5; } //Magic Box 1 if (matrix[0][0] != 2) costA += Math.abs(matrix[0][0]-2); if (matrix[0][1] != 9) costA += Math.abs(matrix[0][1]-9); if (matrix[0][2] != 4) costA += Math.abs(matrix[0][2]-4); if (matrix[1][0] != 7) costA += Math.abs(matrix[1][0]-7); if (matrix[1][1] != 5) costA += Math.abs(matrix[1][1]-5); if (matrix[1][2] != 3) costA += Math.abs(matrix[1][2]-3); if (matrix[2][0] != 6) costA += Math.abs(matrix[2][0]-6); if (matrix[2][1] != 1) costA += Math.abs(matrix[2][1]-1); if (matrix[2][2] != 8) costA += Math.abs(matrix[2][2]-8); //Magic Box 2 if (matrix[0][0] != 4) costB += Math.abs(matrix[0][0]-4); if (matrix[0][1] != 3) costB += Math.abs(matrix[0][1]-3); if (matrix[0][2] != 8) costB += Math.abs(matrix[0][2]-8); if (matrix[1][0] != 9) costB += Math.abs(matrix[1][0]-9); if (matrix[1][1] != 5) costB += Math.abs(matrix[1][1]-5); if (matrix[1][2] != 1) costB += Math.abs(matrix[1][2]-1); if (matrix[2][0] != 2) costB += Math.abs(matrix[2][0]-2); if (matrix[2][1] != 7) costB += Math.abs(matrix[2][1]-7); if (matrix[2][2] != 6) costB += Math.abs(matrix[2][2]-6); //Magic Box 3 if (matrix[0][0] != 8) costC += Math.abs(matrix[0][0]-8); if (matrix[0][1] != 1) costC += Math.abs(matrix[0][1]-1); if (matrix[0][2] != 6) costC += Math.abs(matrix[0][2]-6); if (matrix[1][0] != 3) costC += Math.abs(matrix[1][0]-3); if (matrix[1][1] != 5) costC += Math.abs(matrix[1][1]-5); if (matrix[1][2] != 7) costC += Math.abs(matrix[1][2]-7); if (matrix[2][0] != 4) costC += Math.abs(matrix[2][0]-4); if (matrix[2][1] != 9) costC += Math.abs(matrix[2][1]-9); if (matrix[2][2] != 2) costC += Math.abs(matrix[2][2]-2); //Magic Box 4 if (matrix[0][0] != 6) costD += Math.abs(matrix[0][0]-6); if (matrix[0][1] != 7) costD += Math.abs(matrix[0][1]-7); if (matrix[0][2] != 2) costD += Math.abs(matrix[0][2]-2); if (matrix[1][0] != 1) costD += Math.abs(matrix[1][0]-1); if (matrix[1][1] != 5) costD += Math.abs(matrix[1][1]-5); if (matrix[1][2] != 9) costD += Math.abs(matrix[1][2]-9); if (matrix[2][0] != 8) costD += Math.abs(matrix[2][0]-8); if (matrix[2][1] != 3) costD += Math.abs(matrix[2][1]-3); if (matrix[2][2] != 4) costD += Math.abs(matrix[2][2]-4); //Magic Box 5 if (matrix[0][2] != 2) costE += Math.abs(matrix[0][2]-2); if (matrix[0][1] != 9) costE += Math.abs(matrix[0][1]-9); if (matrix[0][0] != 4) costE += Math.abs(matrix[0][0]-4); if (matrix[1][2] != 7) costE += Math.abs(matrix[1][2]-7); if (matrix[1][1] != 5) costE += Math.abs(matrix[1][1]-5); if (matrix[1][0] != 3) costE += Math.abs(matrix[1][0]-3); if (matrix[2][2] != 6) costE += Math.abs(matrix[2][2]-6); if (matrix[2][1] != 1) costE += Math.abs(matrix[2][1]-1); if (matrix[2][0] != 8) costE += Math.abs(matrix[2][0]-8); //Magic Box 6 if (matrix[0][2] != 4) costF += Math.abs(matrix[0][2]-4); if (matrix[0][1] != 3) costF += Math.abs(matrix[0][1]-3); if (matrix[0][0] != 8) costF += Math.abs(matrix[0][0]-8); if (matrix[1][2] != 9) costF += Math.abs(matrix[1][2]-9); if (matrix[1][1] != 5) costF += Math.abs(matrix[1][1]-5); if (matrix[1][0] != 1) costF += Math.abs(matrix[1][0]-1); if (matrix[2][2] != 2) costF += Math.abs(matrix[2][2]-2); if (matrix[2][1] != 7) costF += Math.abs(matrix[2][1]-7); if (matrix[2][0] != 6) costF += Math.abs(matrix[2][0]-6); //Magic Box 7 if (matrix[0][2] != 8) costG += Math.abs(matrix[0][2]-8); if (matrix[0][1] != 1) costG += Math.abs(matrix[0][1]-1); if (matrix[0][0] != 6) costG += Math.abs(matrix[0][0]-6); if (matrix[1][2] != 3) costG += Math.abs(matrix[1][2]-3); if (matrix[1][1] != 5) costG += Math.abs(matrix[1][1]-5); if (matrix[1][0] != 7) costG += Math.abs(matrix[1][0]-7); if (matrix[2][2] != 4) costG += Math.abs(matrix[2][2]-4); if (matrix[2][1] != 9) costG += Math.abs(matrix[2][1]-9); if (matrix[2][0] != 2) costG += Math.abs(matrix[2][0]-2); //Magic Box 8 if (matrix[0][2] != 6) costH += Math.abs(matrix[0][2]-6); if (matrix[0][1] != 7) costH += Math.abs(matrix[0][1]-7); if (matrix[0][0] != 2) costH += Math.abs(matrix[0][0]-2); if (matrix[1][2] != 1) costH += Math.abs(matrix[1][2]-1); if (matrix[1][1] != 5) costH += Math.abs(matrix[1][1]-5); if (matrix[1][0] != 9) costH += Math.abs(matrix[1][0]-9); if (matrix[2][2] != 8) costH += Math.abs(matrix[2][2]-8); if (matrix[2][1] != 3) costH += Math.abs(matrix[2][1]-3); if (matrix[2][0] != 4) costH += Math.abs(matrix[2][0]-4); lowestCost = Math.min(Math.min(Math.min(Math.min(Math.min(Math.min((Math.min(costA,costB)),costC),costD),costE),costF),costG),costH); //Print result System.out.println(lowestCost); } }