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) { Scanner in = new Scanner (System.in); /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ int n = 3; int cost = -1; int[][] matrix = new int[3][3]; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ matrix[i][j] = in.nextInt(); } } int[][] options={ {2,9,4}, {2,7,6}, {4,3,8}, {4,9,2}, {6,7,2},{6,1,8},{8,1,6}, {8,3,4} }; for (int i =0; i < 8; i++){ int[][] thismatrix = getOption(options[i]); int thiscost = calcCost(matrix,thismatrix); if (cost < 0) { cost = thiscost; } else if (cost > thiscost) { cost = thiscost; } } System.out.println(cost); } static int[][] getOption (int[] opt) { int[][] option = new int [3][3]; option[0] = opt; option[1][1] = 5; for (int i =0; i < 3; i++){ option[2][i] = 10 - opt[2-i]; } option[1][0] = 15 - option[0][0] - option[2][0]; option[1][2] = 15 - option[0][2] - option[2][2]; return (option); } static int calcCost (int[][] inMatrix, int[][]destMatrix){ int cost = 0; for (int i = 0; i < 3; i++) { for (int j =0; j < 3; j++){ cost = cost + Math.abs(inMatrix[i][j]-destMatrix[i][j]); } } return (cost); } }