import java.io.*; import java.util.*; 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 scan = new Scanner(System.in); int n = 9; int k = 8; int[][] arrayCheck = new int[k][n]; int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = scan.nextInt(); } arrayCheck[0] = new int[] { 2, 9, 4, 7, 5, 3, 6, 1, 8 } ; arrayCheck[1] = new int[] { 6, 7, 2, 1, 5, 9, 8, 3, 4 } ; arrayCheck[2] = new int[] { 8, 1, 6, 3, 5, 7, 4, 9, 2 } ; arrayCheck[3] = new int[] { 4, 3, 8, 9, 5, 1, 2, 7, 6 } ; arrayCheck[4] = new int[] { 4, 9, 2, 3, 5, 7, 8, 1, 6 } ; arrayCheck[5] = new int[] { 8, 3, 4, 1, 5, 9, 6, 7, 2 } ; arrayCheck[6] = new int[] { 6, 1, 8, 7, 5, 3, 2, 9, 4 } ; arrayCheck[7] = new int[]{ 2, 7, 6, 9, 5, 1, 4, 3, 8 } ; int[] arrayCoast = new int[8]; for(int i = 0; i < k; i++) { int coast = 0; for(int j = 0; j < n; j++){ coast += Math.abs(arrayCheck[i][j] - array[j]); } arrayCoast[i] = coast; } Arrays.sort(arrayCoast); System.out.println(arrayCoast[0]); } }