import java.util.Random; import java.util.Scanner; public class JMain2 { private static int[][] matrix = new int[3][3]; private static int[] checker = new int[10]; private static int index = 0; public static void main(String[] args) { Scanner scan = new Scanner(System.in); String firstRow = scan.nextLine(); String secondRow = scan.nextLine(); String thirdRow = scan.nextLine(); String[] firstRowSplit = firstRow.split("\\s"); String[] secondRowSplit = secondRow.split("\\s"); String[] thirdRowSplit = thirdRow.split("\\s"); for (int i = 0; i < 3; i++) { matrix[0][i] = Integer.parseInt(firstRowSplit[i]); matrix[1][i] = Integer.parseInt(secondRowSplit[i]); matrix[2][i] = Integer.parseInt(thirdRowSplit[i]); } int[][] firstMatrix = { { 8, 1, 6 }, { 3, 5, 7 }, { 4, 9, 2 } }; int[][] secondMatrix = { { 6, 1, 8 }, { 7, 5, 3 }, { 2, 9, 4 } }; int[][] thirdMatrix = { { 4, 9, 2 }, { 3, 5, 7 }, { 8, 1, 6 } }; int[][] fourthMatrix = { { 2, 9, 4 }, { 7, 5, 3 }, { 6, 1, 8 } }; int[][] fifthMatrix = { { 8, 3, 4 }, { 1, 5, 9 }, { 6, 7, 2 } }; int[][] sixthMatrix = { { 4, 3, 8 }, { 9, 5, 1 }, { 2, 7, 6 } }; int[][] sevenMatrix = { { 6, 7, 2 }, { 1, 5, 9 }, { 8, 3, 4 } }; int[][] eigthMatrix = { { 2, 7, 6 }, { 9, 5, 1 }, { 4, 3, 8 } }; minCost(matrix, firstMatrix); minCost(matrix, secondMatrix); minCost(matrix, thirdMatrix); minCost(matrix, fourthMatrix); minCost(matrix, fifthMatrix); minCost(matrix, sixthMatrix); minCost(matrix, sevenMatrix); minCost(matrix, eigthMatrix); int min = 100000; for(int i =0; i < index; i++){ if(checker[i] < min){ min = checker[i]; } } System.out.println(min); } public static void minCost(int[][] first, int[][] second) { int count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (first[i][j] != second[i][j]) { int value = Math.abs(first[i][j] - second[i][j]); count += value; } } } checker[index++] = count; } }