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. */ // The sum must be 15 Integer[] mySquare; Integer[] cmpSquare; Scanner scan = new Scanner(System.in); mySquare = new Integer[9]; int distanceCalculated; ArrayList list = new ArrayList<>(); Integer[] intArray1 = new Integer[]{2,9,4,7,5,3,6,1,8}; list.add(intArray1); Integer[] intArray2 = new Integer[]{6,1,8,7,5,3,2,9,4}; list.add(intArray2); Integer[] intArray3 = new Integer[]{8,1,6,3,5,7,4,9,2}; list.add(intArray3); Integer[] intArray4 = new Integer[]{4,9,2,3,5,7,8,1,6}; list.add(intArray4); Integer[] intArray5 = new Integer[]{6,7,2,1,5,9,8,3,4}; list.add(intArray5); Integer[] intArray6 = new Integer[]{8,3,4,1,5,9,6,7,2}; list.add(intArray6); Integer[] intArray7 = new Integer[]{2,7,6,9,5,1,4,3,8}; list.add(intArray7); Integer[] intArray8 = new Integer[]{4,3,8,9,5,1,2,7,6}; list.add(intArray8); for (int pos = 0; pos < 9; pos++) { mySquare[pos] = scan.nextInt(); } Iterator it = list.iterator(); int minDistance = 1000; while (it.hasNext()) { cmpSquare = (Integer[])it.next(); distanceCalculated = calcDistanceArrays(cmpSquare,mySquare); if (minDistance > distanceCalculated) { minDistance = distanceCalculated; } } System.out.printf("%d",minDistance); } private static int calcDistanceArrays (Integer[] a1, Integer[] a2) { int dist = 0; for (int i=0; i