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) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner in = new Scanner(System.in); int a[][] = new int[3][3]; int arr1[][] = {{8,1,6},{3,5,7},{4,9,2}}; int arr2[][] = {{4,3,8},{9,5,1},{2,7,6}}; int arr3[][] = {{2,9,4},{7,5,3},{6,1,8}}; int arr4[][] = {{6,7,2},{1,5,9},{8,3,4}}; int arr5[][] = {{6,1,8},{7,5,3},{2,9,4}}; int arr6[][] = {{8,3,4},{1,5,9},{6,7,2}}; int arr7[][] = {{4,9,2},{3,5,7},{8,1,6}}; int arr8[][] = {{2,7,6},{9,5,1},{4,3,8}}; ArrayList result = new ArrayList(); for (int i = 0; i < 8; i++) { result.add(0); } int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0, sum6 = 0, sum7 = 0, sum8 = 0; for(int a_i=0; a_i < 3; a_i++){ for(int a_j=0; a_j < 3; a_j++){ int num = in.nextInt(); if (num != arr1[a_i][a_j]) { sum1 += Math.abs(num - arr1[a_i][a_j]); result.set(0,sum1); } if (num != arr2[a_i][a_j]){ sum2 += Math.abs(num - arr2[a_i][a_j]); result.set(1,sum2); } if (num != arr3[a_i][a_j]){ sum3 += Math.abs(num - arr3[a_i][a_j]); result.set(2,sum3); } if (num != arr4[a_i][a_j]) { sum4 += Math.abs(num - arr4[a_i][a_j]); result.set(3,sum4); } if (num != arr5[a_i][a_j]){ sum5 += Math.abs(num - arr5[a_i][a_j]); result.set(4,sum5); } if (num != arr6[a_i][a_j]) { sum6 += Math.abs(num - arr6[a_i][a_j]); result.set(5,sum6); } if (num != arr7[a_i][a_j]) { sum7 += Math.abs(num - arr7[a_i][a_j]); result.set(6,sum7); } if (num != arr8[a_i][a_j]) { sum8 += Math.abs(num - arr8[a_i][a_j]); result.set(7,sum8); } } } Collections.sort(result); System.out.println(result.get(0)); } }