Problem: A permutation of N numbers from 1 to N A1, A2, ……, AN is given. You can perform the operation reverse (i, j) to reverse the order of the numbers in the interval [i, j] (1 ≤ i ≤ j ≤ N) for this permutation. For example, if reverse (2, 4) is applied to [1, 2, 3, 4, 5], it becomes [1, 4, 3, 2, 5]. Calculate how many operations you need to do at a minimum to get the permutation sorted in ascending order.

Constraints

* N is an integer

* 2 ≤ N ≤ 10

Input

The input is given in the following format.

> N
> A1 A2 …… AN
>

Output

Print the solution to the problem on one line.

Examples

Input

5
1 4 3 5 2


Output

2


Input

5
3 1 5 2 4


Output

4


Input

3
1 2 3


Output

0


Input

10
3 1 5 2 7 4 9 6 10 8


Output

9

Solution (in CPP):

Solution:
#include <iostream>
#include <map>
#include <queue>
#include <algorithm>

using namespace std;
typedef long long ll;

struct S {
	int n;
	char d[10];
	S() {}
	S(int n) : n(n) {}
	ll hash() {
		ll r = 0;
		for (int i = 0; i < n; i++) {
			r *= 16;
			r += d[i];
		}
		return r;
	}
};

typedef pair<S, int> P;

int main() {
	int n;
	cin >> n;
	S s(n), r(n);
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x; x--;
		s.d[i] = x;
		r.d[i] = i;
	}
	ll rh = r.hash();
	int dist = 1, rdist = 1;
	queue<P> q;
	map<ll, int> u;
	q.push(P(s, 1));
	while (!q.empty()) {
		S s(0); int d;
		tie(s, d) = q.front(); q.pop();
		if (d == 6) break;

		ll sh = s.hash();
		if (u[sh]) continue;
		u[sh] = d;
		dist = d;
		if (sh == rh) {
			break;
		}
		for (int i = 0; i < n; i++) {
			for (int j = i+2; j <= n; j++) {
				S ss = s;
				reverse(ss.d + i, ss.d + j);
				q.push(P(ss, d+1));
			}
		}
	}
	map<ll, int> ru;
	q = queue<P>();
	q.push(P(r, 1));
	while (!q.empty()) {
		S s(0); int d;
		tie(s, d) = q.front(); q.pop();
		ll sh = s.hash();
		if (d == 6) {
			rdist = 6;
			break;
		}
		if (ru[sh]) continue;
		ru[sh] = d;
		rdist = d;
		if (u[sh]) {
			break;
		}
		for (int i = 0; i < n; i++) {
			for (int j = i+2; j <= n; j++) {
				S ss = s;
				reverse(ss.d + i, ss.d + j);
				q.push(P(ss, d+1));
			}
		}
	}
	cout << dist+rdist-2 << endl;
	return 0;
}