import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int cost = 0; static int arr[] = new int[9]; public static void main(String[] args) { Scanner in = new Scanner(System.in); for(int i = 0; i < 9; i++) { arr[i] = in.nextInt(); } magicArray(); } static void magicArray() { int magic[][] = new int[][]{{2, 4}, {2, 6}, {8, 4}, {8, 6}, {4, 2}, {4, 8}, {6, 2}, {6, 8}}; int array[] = new int[9]; int minCost = Integer.MAX_VALUE; for (int[] x : magic) { int cost = 0; array[4] = 5; array[0] = x[0]; array[8] = 10 - x[0]; array[2] = x[1]; array[6] = 10 - x[1]; array[1] = 15 - array[0] - array[2]; array[3] = 15 - array[0] - array[6]; array[5] = 15 - array[2] - array[8]; array[7] = 15 - array[6] - array[8]; for(int i = 0; i < 9;i++) { cost += Math.abs(array[i] - arr[i]); } if (cost < minCost) { minCost = cost; } } System.out.println(minCost); } }