import java.util.*; public class _2342 { static int INF = Integer.MAX_VALUE; static int[][] move_energy = { {0,2,2,2,2}, {0,1,3,4,3}, {0,3,1,3,4}, {0,4,3,1,3}, {0,3,4,3,1} }; static int[][][] dp = new int[2][5][5]; static int curr = 0, next = 1; static void swap() { int temp = curr; curr = next; next = temp; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] inst = Arrays.stream(sc.nextLine().split(" ")) .mapToInt(Integer::parseInt) .toArray(); sc.close(); for(int i=0; i<2; i++) { for(int l=0; l<5; l++) { for(int r=0; r<5; r++) { if(i==0) dp[i][l][r] = INF; else dp[i][l][r] = 0; } } } for(int i=inst.length-2; i>=0; i--) { int target = inst[i]; for(int L=0; L<5; L++) { for(int R=0; R<5; R++) { int moveLeft = dp[next][target][R] + move_energy[L][target]; int moveRight = dp[next][L][target] + move_energy[R][target]; dp[curr][L][R] = Math.min(moveLeft, moveRight); } } swap(); } System.out.println(dp[next][0][0]); } }