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[][] matrix = new int[3][3]; Scanner sc = new Scanner(System.in); for(int r=0; r<3; r++){ for(int c=0; c<3; c++){ matrix[r][c] = sc.nextInt(); } } int minCost = Integer.MAX_VALUE; int[] corners1 = new int[]{2,4,8,6}; int[] sides1 = new int[]{9,3,1,7}; int[] corners2 = new int[]{2,6,8,4}; int[] sides2 = new int[]{7,1,3,9}; int currentCost = 0; for(int i=0; i<4; i++){ currentCost = 0; currentCost += Math.abs(matrix[1][1] - 5); currentCost += Math.abs(matrix[0][0] - corners1[(i + 4) % 4]); currentCost += Math.abs(matrix[1][0] - sides1[(i + 4) % 4]); currentCost += Math.abs(matrix[2][0] - corners1[((i + 1) + 4) % 4]); currentCost += Math.abs(matrix[2][1] - sides1[((i + 1) + 4) % 4]); currentCost += Math.abs(matrix[2][2] - corners1[((i + 2) + 4) % 4]); currentCost += Math.abs(matrix[1][2] - sides1[((i + 2) + 4) % 4]); currentCost += Math.abs(matrix[0][2] - corners1[((i + 3) + 4) % 4]); currentCost += Math.abs(matrix[0][1] - sides1[((i + 3) + 4) % 4]); if(currentCost < minCost){ minCost = currentCost; } } for(int i=0; i<4; i++){ currentCost = 0; currentCost += Math.abs(matrix[1][1] - 5); currentCost += Math.abs(matrix[0][0] - corners2[(i + 4) % 4]); currentCost += Math.abs(matrix[1][0] - sides2[(i + 4) % 4]); currentCost += Math.abs(matrix[2][0] - corners2[((i + 1) + 4) % 4]); currentCost += Math.abs(matrix[2][1] - sides2[((i + 1) + 4) % 4]); currentCost += Math.abs(matrix[2][2] - corners2[((i + 2) + 4) % 4]); currentCost += Math.abs(matrix[1][2] - sides2[((i + 2) + 4) % 4]); currentCost += Math.abs(matrix[0][2] - corners2[((i + 3) + 4) % 4]); currentCost += Math.abs(matrix[0][1] - sides2[((i + 3) + 4) % 4]); if(currentCost < minCost){ minCost = currentCost; } } System.out.println(minCost); } }