import java.io.*; import java.util.*; public class Solution { private static int[][][] generateMagicSquares() { int[][][] magicSquares = new int[8][][]; magicSquares[0] = new int[][] {{2,7,6},{9,5,1},{4,3,8}}; for (int i = 0; i < 3; i++) { magicSquares[i + 1] = rotate(magicSquares[i]); } for (int i = 0; i < 4; i++) { magicSquares[4 + i] = mirror(magicSquares[i]); } return magicSquares; } private static int[][] rotate(int[][] m) { int[][] ret = new int[3][3]; ret[0][0] = m[2][0]; ret[0][1] = m[1][0]; ret[0][2] = m[0][0]; ret[1][0] = m[2][1]; ret[1][1] = m[1][1]; ret[1][2] = m[0][1]; ret[2][0] = m[2][2]; ret[2][1] = m[1][2]; ret[2][2] = m[0][2]; return ret; } private static int[][] mirror(int[][] m) { int[][] ret = new int[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ret[i][j] = m[i][2-j]; } } return ret; } private static int distance(int[][] m, int[][] s) { int ret = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ret += Math.abs(m[i][j] - s[i][j]); } } return ret; } public static void main(String[] args) { int[][][] magicSquares = generateMagicSquares(); /* * Enter your code here. Read input from STDIN. Print output to STDOUT. * Your class should be named Solution. */ Scanner scan = new Scanner(System.in); int[][] square = new int[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { square[i][j] = scan.nextInt(); } } int minDistance = Integer.MAX_VALUE; for (int i = 0; i < magicSquares.length; i++) { minDistance = Math.min(minDistance, distance(square, magicSquares[i])); } System.out.println(minDistance); } private static void printSquare(int[][] square) { int[] rowSum = new int [3]; int[] colSum = new int [3]; int rightDiagonalTotal = 0; int leftDiagonalTotal = 0; for (int i = 0; i < 3; i++) { rightDiagonalTotal = 0; leftDiagonalTotal = 0; for (int j = 0; j < 3; j++) { rowSum[i] += square[i][j]; colSum[i] += square[j][i]; rightDiagonalTotal += square[j][j]; leftDiagonalTotal += square[2-j][j]; } } for (int i = 0; i < 3; i++) { System.out.print(" "); for(int j = 0; j < 3; j++) { System.out.print(square[i][j] + " "); } System.out.println(rowSum[i]); } System.out.print(leftDiagonalTotal + " "); for(int j = 0; j < 3; j++) { System.out.print(colSum[j] + " "); } System.out.println(rightDiagonalTotal + " "); } }