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>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

const int M=3628800;
int n;

int g(vi a){
	int res=0,tmp=1;
	for(int i=n-2;i>=0;i--){
		int cnt=0;
		for(int j=i+1;j<n;j++) if(a[i]>a[j]) cnt++;
		res+=cnt*tmp;
		tmp*=n-i;
	}
	return res;
}

vi a;

vi f(){
	queue<pair<vi,int>> q;
	q.push(make_pair(a,0));
	vi dis(M,inf);
	dis[g(a)]=0;
	while(!q.empty()){
		auto p=q.front();q.pop();
		vi b=p.first;
		int d=p.second;
		for(int i=0;i<n;i++) for(int j=i+2;j<=n;j++){
			reverse(b.begin()+i,b.begin()+j);
			int id=g(b);
			if(dis[id]==inf){
				dis[id]=d+1;
				if(d+1<4){
					q.push(make_pair(b,d+1));
				}
			}
			reverse(b.begin()+i,b.begin()+j);
		}
	}
	return dis;
}

int main(){
	cin>>n;
	a=vi(n);
	for(auto &i:a) cin>>i;
	vi b=f();
	sort(a.begin(),a.end());
	vi c=f();
	int res=9;
	for(int i=0;i<M;i++) res=min(res,b[i]+c[i]);
	cout<<res<<endl;
}