import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { // gonna hazard a guess that there is a much more elegant solution public static void main(String[] args) { Scanner sc = new Scanner(System.in); int cost = 0; int cost2 = 0; int cost3 = 0; int cost4 = 0; int cost5 = 0; int cost6 = 0; int cost7 = 0; int cost8 = 0; int[] ar = {4, 9, 2, 3, 5, 7, 8, 1, 6}; int[] ar2 = {2, 9, 4, 7, 5, 3, 6, 1, 8}; int[] ar3 = {8, 1, 6, 3, 5, 7, 4, 9, 2}; int[] ar4 = {2, 7, 6, 9, 5, 1, 4, 3 , 8}; int[] ar5 = {6, 1, 8, 7, 5, 3, 2, 9, 4}; int[] ar6 = {8, 3, 4, 1, 5, 9, 6, 7, 2}; int[] ar7 = {4, 3, 8, 9, 5, 1, 2, 7, 6}; int[] ar8 = {6, 7, 2, 1, 5, 9, 8, 3, 4}; for(int i = 0; i < ar.length; i++){ int a = sc.nextInt(); int b = ar[i]; int c = ar2[i]; int d = ar3[i]; int e = ar4[i]; int f = ar5[i]; int g = ar6[i]; int h = ar7[i]; int k = ar8[i]; cost = cost + Math.abs(a - b); cost2 = cost2 + Math.abs(a - c); cost3 = cost3 + Math.abs(a - d); cost4 = cost4 + Math.abs(a - e); cost5 = cost5 + Math.abs(a - f); cost6 = cost6 + Math.abs(a - g); cost7 = cost7 + Math.abs(a - h); cost8 = cost8 + Math.abs(a - k); } int acost = Math.min(cost, Math.min(cost2, Math.min(cost3, Math.min(cost4, Math.min(cost5, Math.min(cost6, Math.min(cost7, cost8))))))); System.out.println(acost); } }