import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int[] s1 = new int[]{8,1,6,3,5,7,4,9,2}; static int[] s2 = new int[]{6,1,8,7,5,3,2,9,4}; static int[] s3 = new int[]{8,3,4,1,5,9,6,7,2}; static int[] s4 = new int[]{4,3,8,9,5,1,2,7,6}; static int[] s5 = new int[]{6,7,2,1,5,9,8,3,4}; static int[] s6 = new int[]{2,7,6,9,5,1,4,3,8}; static int[] s7 = new int[]{4,9,2,3,5,7,8,1,6}; static int[] s8 = new int[]{2,9,4,7,5,3,6,1,8}; public static int change(int[] arr, int[] solution) { int res = 0; for (int i = 0; i < 9; i++) { res += Math.abs(arr[i] - solution[i]); } return res; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = new int[9]; for (int i = 0; i < 9; i++) { arr[i] = sc.nextInt(); } int[] f = new int[]{change(arr, s1), change(arr, s2), change(arr, s3), change(arr, s4), change(arr, s5), change(arr, s6), change(arr, s7), change(arr, s8)}; Arrays.sort(f); System.out.println(f[0]); } }