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 <bits/stdc++.h>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) reep((i),0,(n))
#define PB push_back
#define mkp make_pair
#define mins(a,b) a=min(a,b)

typedef long long ll;
typedef vector<int> vint;

map<ll, int> ma;
set<ll> se;



ll pow1[10];
ll pow2[10];

void rev(ll &a,int l,int r){
	while(l<r){
		ll b = a%pow2[l]/pow1[l]*pow1[l];
		ll c = a%pow2[r]/pow1[r]*pow1[r];
		a+=-b-c+b*pow1[r-l]+c/pow1[r-l];
		l++;
		r--;
	}
}

vector<int> w;


int main() {
	int n;
	cin >> n;
	vector<int> v(n);
	rep(i, n) cin >> v[i], v[i]--;
	w.assign(n, 0);
	rep(i, n) w[i] = i;
	if(v == w) {
		cout << 0 << endl;
		return 0;
	}
	pow1[0] = 1;
	pow2[0] = 10;
	rep(i,9){
		pow1[i+1] = pow1[i]*10;
		pow2[i+1] = pow2[i]*10;
	}
	int ans = n - 1;
	queue<ll> q[2];
	ll vv=0,ww=0;
	rep(i,n){
		vv=vv*10+v[i];
		ww=ww*10+w[i];
	}
	q[0].push(vv);
	int m = (v.size()-1)/2;
	ma[vv] = 0;
	rep(o,m){
		queue<ll>& now = q[o%2];
		queue<ll>& next = q[1-o%2];
		while(now.size()){
			auto top = now.front();
			now.pop();
			rep(i,n){
				rep(j,i){
					rev(top,j,i);
					if(ma.count(top)==0){
						ma[top] = o+1;
						next.push(top);
					}
					rev(top,j,i);
				}
			}
		}
	}
	while(q[0].size()) q[0].pop();
	while(q[1].size()) q[1].pop();
	set<ll> used;
	q[0].push(ww);
	rep(o,m){
		queue<ll>& now = q[o%2];
		queue<ll>& next = q[1-o%2];
		while(now.size()){
			auto top = now.front();
			now.pop();
			rep(i,n){
				rep(j,i){
					rev(top,j,i);
					if(ma.count(top)){
						mins(ans,ma[top]+o+1);
					}
					else if(used.find(top)!=used.end()){

					}
					else{
						next.push(top);
						used.insert(top);
					}
					rev(top,j,i);
				}
			}
		}
	}
	cout << ans << endl;
}