import java.util.*; public class Solution { private static int[][] getMagicSquare(int i) { switch(i) { case 1: return new int[][]{ {8, 1, 6}, {3, 5, 7}, {4, 9, 2} }; case 2: return new int[][]{ {4, 3, 8}, {9, 5, 1}, {2, 7, 6} }; case 3: return new int[][]{ {2, 9, 4}, {7, 5, 3}, {6, 1, 8} }; case 4: return new int[][]{ {6, 7, 2}, {1, 5, 9}, {8, 3, 4} }; case 5: return new int[][]{ {6, 1, 8}, {7, 5, 3}, {2, 9, 4} }; case 6: return new int[][]{ {8, 3, 4}, {1, 5, 9}, {6, 7, 2} }; case 7: return new int[][]{ {4, 9, 2}, {3, 5, 7}, {8, 1, 6} }; case 8: return new int[][]{ {2, 7, 6}, {9, 5, 1}, {4, 3, 8} }; } return null; } private static int compare(int[][] inSquare, int k) { int[][] compSquare = getMagicSquare(k); int retVal = 0; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { retVal += Math.abs(inSquare[i][j]-compSquare[i][j]); } } return retVal; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int[][] inSquare = new int[3][3]; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { inSquare[i][j] = in.nextInt(); } } int minCost = Integer.MAX_VALUE; for(int i=1; i<=8; i++) { int cost = compare(inSquare, i); if (cost < minCost) minCost = cost; } System.out.println(minCost); } }