import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static final int ps1[][] = { {4, 9, 2}, {3, 5, 7}, {8, 1, 6} }; public static final int ps2[][] = { {8, 1, 6}, {3, 5, 7}, {4, 9, 2} }; public static final int ps3[][] = { {6, 7, 2}, {1, 5, 9}, {8, 3, 4} }; public static final int ps4[][] = { {2, 9, 4}, {7, 5, 3}, {6, 1, 8} }; public static final int ps5[][] = { {4, 3, 8}, {9, 5, 1}, {2, 7, 6} }; public static final int ps6[][] = { {8, 3, 4}, {1, 5, 9}, {6, 7, 2} }; public static final int ps7[][] = { {2, 7, 6}, {9, 5, 1}, {4, 3, 8} }; public static final int ps8[][] = { {6, 1, 8}, {7, 5, 3}, {2, 9, 4} }; public static final int perfectSquares[][][] = {ps1,ps2,ps3,ps4,ps5,ps6,ps7,ps8}; public static void main(String[] args) { Scanner scan = new Scanner(System.in); int s[][] = new int[3][3]; for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ s[i][j] = scan.nextInt(); } } int minCost = calculateSquareChangeCost(s,ps1); for(int i = 1; i < 8; i++){ int cost = calculateSquareChangeCost(s, perfectSquares[i]); if(cost < minCost){ minCost = cost; } } System.out.println(minCost); } private static int calculateSquareChangeCost(int a[][], int b[][]){ return Math.abs(a[0][0] - b[0][0]) + Math.abs(a[1][0] - b[1][0]) + Math.abs(a[2][0] - b[2][0]) + Math.abs(a[0][1] - b[0][1]) + Math.abs(a[1][1] - b[1][1]) + Math.abs(a[2][1] - b[2][1]) + Math.abs(a[0][2] - b[0][2]) + Math.abs(a[1][2] - b[1][2]) + Math.abs(a[2][2] - b[2][2]); } }