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) { int[][] magicSquare = { {2, 7, 6}, {9, 5, 1}, {4, 3, 8} }; // magicSquare = rotateRight(magicSquare); // printMatrix(magicSquare); int[][] square = makeMatrix(new Scanner(System.in)); int n = 3; int minimum = Integer.MAX_VALUE; for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) { minimum = Math.min(countDiff(magicSquare, square), minimum); magicSquare = rotateRight(magicSquare); } magicSquare = reflectDown(magicSquare); } System.out.println(minimum); } public static void printMatrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } public static int[][] makeMatrix(Scanner scan) { int n = 3; int[][] square = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { square[i][j] = scan.nextInt(); } } return square; } public static int countDiff(int[][] magicSquare, int[][] input) { int n = 3; int diff = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { diff += Math.abs(magicSquare[i][j] - input[i][j]); } } return diff; } public static int[][] rotateRight(int[][] matrix) { int n = 3; int[][] rotated = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { rotated[i][j] = matrix[n - j - 1][i]; } } return rotated; } public static int[][] reflectDown(int[][] matrix) { int n = 3; for (int i = 0; i < n; i++) { for (int j = 0; j < n / 2; j++) { int temp = matrix[j][i]; matrix[j][i] = matrix[n - j - 1][i]; matrix[n - j - 1][i] = temp; } } return matrix; } }