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. */ ArrayList validSquares = new ArrayList(); int[] v = {4, 9, 2, 3, 5, 7, 8, 1, 6}; validSquares.add(v); v = new int[]{8, 3, 4, 1, 5, 9, 6, 7, 2}; validSquares.add(v); v = new int[]{6, 1, 8, 7, 5, 3, 2, 9, 4}; validSquares.add(v); v = new int[]{2, 7, 6, 9, 5, 1, 4, 3, 8}; validSquares.add(v); v = new int[]{2, 9, 4, 7, 5, 3, 6, 1, 8}; validSquares.add(v); v = new int[]{8, 1, 6, 3, 5, 7, 4, 9, 2}; validSquares.add(v); v = new int[]{6, 7, 2, 1, 5, 9, 8, 3, 4}; validSquares.add(v); v = new int[]{4, 3, 8, 9, 5, 1, 2, 7, 6}; validSquares.add(v); /*validSquares[1] = validSquares[2] = validSquares[3] = validSquares[4] = validSquares[5] = validSquares[6] = validSquares[7] = */ Scanner sc = new Scanner(System.in); int[] square = new int[9]; for (int i = 0; i < 9; i++) { square[i] = sc.nextInt(); } int cost; int minCost = Integer.MAX_VALUE; for (int[] valid : validSquares) { cost = 0; for (int i = 0; i < 9; i++) { cost += Math.abs(square[i] - valid[i]); } if (cost < minCost) { minCost = cost; } } System.out.println(minCost); sc.close(); } }