import java.io.*; import java.util.*; public class Solution { public static int compare(Integer[] a, Integer[] b){ int sum = 0; for (int i = 0; i < a.length; i++){ sum += Math.abs(a[i] - b[i]); } return sum; } 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); Integer[] a = new Integer[9]; for (int i = 0; i < a.length; i++){ a[i] = scan.nextInt(); } ArrayList m_squares = new ArrayList(); m_squares.add(new Integer[]{4, 9, 2, 3, 5, 7, 8, 1, 6}); m_squares.add(new Integer[]{2, 7, 6, 9, 5, 1, 4, 3, 8}); m_squares.add(new Integer[]{6, 1, 8, 7, 5, 3, 2, 9, 4}); m_squares.add(new Integer[]{8, 3, 4, 1, 5, 9, 6, 7, 2}); m_squares.add(new Integer[]{2, 9, 4, 7, 5, 3, 6, 1, 8}); m_squares.add(new Integer[]{6, 7, 2, 1, 5, 9, 8, 3, 4}); m_squares.add(new Integer[]{8, 1, 6, 3, 5, 7, 4, 9, 2}); m_squares.add(new Integer[]{4, 3, 8, 9, 5, 1, 2, 7, 6}); int min_value = 1000; for (Integer[] square: m_squares){ int value = compare(square, a); if (value < min_value){ min_value = value; } } System.out.println(min_value); } }