// Arup Guha
// 2/13/2017
// Solution to German Programming Contest Problem B: Correcting Cheeseburgers

import java.util.*;

public class b {

	public static void main(String[] args) {

		// Get the input.
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		String start = "";
		for (int i=0; i<n; i++)
			start =  start + (char)(stdin.nextInt()-1+'0');

		// Get a list of all cheeseburgers possible 3 steps from the end.
		HashMap mapEnd = getMapEnd(done(n));

		// Now, BFS from our start position until we hit anything in mapEnd.
		System.out.println(bfs(start, mapEnd));
	}

	// Runs a BFS from start to anything in mapEnd.
	public static int bfs(String start, HashMap<String,Integer> mapEnd) {

		int n = start.length();

		LinkedList<String> q = new LinkedList<String>();
		HashMap<String,Integer> mapStart = new HashMap<String,Integer>();

		// Start our BFS from start and try to hit something in mapEnd.
		q.offer(start);
		mapStart.put(start, 0);

		// This should still work.
		while (q.size() > 0) {

			// Get the next item.
			String cur = q.poll();
			int dist = mapStart.get(cur);

			// We have met in the middle, return!
			if (mapEnd.containsKey(cur)) return dist + mapEnd.get(cur);

			// Kevin Bacon idea! 6 is max, so if this is 3, then we know the answeer must be 6, even if
			// we haven't found the specific path.
			if (dist == 3) return 6;

			// Get all the next possible cheeseburgers.
			ArrayList<String> next = getNext(cur);

			for (int i=0; i<next.size(); i++) {

				// We've built this one before.
				if (mapStart.containsKey(next.get(i))) continue;

				// Add our distance to the hashmap and add this burger to the queue.
				mapStart.put(next.get(i), dist+1);
				q.offer(next.get(i));
			}
		}

		return -1;
	}

	// Returns everything we can reach from end, going backwards in 3 steps.
	public static HashMap<String,Integer> getMapEnd(String end) {

		// Set up BFS backwards.
		int n = end.length();
		LinkedList<String> q = new LinkedList<String>();
		HashMap<String,Integer> map = new HashMap<String,Integer>();
		q.offer(end);
		map.put(end, 0);

		// This should still work.
		while (q.size() > 0) {

			// Get the next item.
			String cur = q.poll();
			int dist = map.get(cur);

			// Not building off of these.
			if (dist == 3) continue;

			// Get the next cheeseburgers we can make, going backwards from here.
			ArrayList<String> next = getNextBackwards(cur);

			for (int i=0; i<next.size(); i++) {

				// Been here before, so skip it.
				if (map.containsKey(next.get(i))) continue;

				// Add to the queue and put in the map.
				map.put(next.get(i), dist+1);
				q.offer(next.get(i));
			}
		}

		return map;
	}

	// Returns all possible next strings to s, moving forward.
	public static ArrayList<String> getNext(String s) {

		ArrayList<String> res = new ArrayList<String>();
		int n = s.length();

		// Here are out break points.
		for (int a=0; a<=n; a++) {
			for (int b=a; b<=n; b++) {
				for (int c=b; c<=n; c++) {

					// Slice the string up.
					String one = s.substring(0,a);
					String two = s.substring(a,b);
					String three = s.substring(b,c);
					String four = s.substring(c,n);

					// Put back together in the order they want.
					res.add(three+one+four+two);
				}
			}
		}
		return res;
	}

	// Returns all the possible strings moving backwards from s.
	public static ArrayList<String> getNextBackwards(String s) {

		ArrayList<String> res = new ArrayList<String>();
		int n = s.length();

		// Try all break points.
		for (int a=0; a<=n; a++) {
			for (int b=a; b<=n; b++) {
				for (int c=b; c<=n; c++) {

					// Slice up the string.
					String one = s.substring(0,a);
					String two = s.substring(a,b);
					String three = s.substring(b,c);
					String four = s.substring(c,n);

					// This is the way we put it together backwards.
					res.add(two+four+one+three);
				}
			}
		}
		return res;
	}

	public static String done(int n) {
		String res = "";
		for (int i=0; i<n; i++)
			res = res + (char)('0'+i);
		return res;
	}


}