import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] input = new int[9]; for(int i = 0; i < 4; i++){ input[i]= scanner.nextInt(); } int center = scanner.nextInt(); for(int i = 4; i < 8; i++){ input[i]= scanner.nextInt(); } scanner.close(); int squares[][]={{8,1,6,3,7,4,9,2}, {6,1,8,7,3,2,9,4}, {4,9,2,3,7,8,1,6}, {2,9,4,7,3,6,1,8}, {8,3,4,1,9,6,7,2}, {4,3,8,9,1,2,7,6}, {6,7,2,1,9,8,3,4}, {2,7,6,9,1,4,3,8}}; int minCost = 10000; //for every square i for(int i = 0; i < 8; i++) { int cost = 0; //for every field j for(int j = 0; j < 8; j++) { cost += Math.abs(input[j] - squares[i][j]); } if(cost < minCost) { minCost = cost; } } int costCenter = Math.abs(center - 5); System.out.println(minCost + costCenter); } }