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 arr[] = new int[9]; 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}; int score1 = 0; int score2 = 0; int score3 = 0; int score4 = 0; int score5 = 0; int score6 = 0; int score7 = 0; int score8 = 0; for(int arr_i=0; arr_i < 9; arr_i++){ arr[arr_i] = in.nextInt(); score1 += Math.abs(arr[arr_i] - arr1[arr_i]); score2 += Math.abs(arr[arr_i] - arr2[arr_i]); score3 += Math.abs(arr[arr_i] - arr3[arr_i]); score4 += Math.abs(arr[arr_i] - arr4[arr_i]); score5 += Math.abs(arr[arr_i] - arr5[arr_i]); score6 += Math.abs(arr[arr_i] - arr6[arr_i]); score7 += Math.abs(arr[arr_i] - arr7[arr_i]); score8 += Math.abs(arr[arr_i] - arr8[arr_i]); } int arrsort[] = {score1, score2, score3, score4, score5, score6, score7, score8}; Arrays.sort(arrsort); System.out.println(arrsort[0]); } }