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;

const long long int ten[10]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};

long long int coder(vector<int> &vec){
	long long int res=0;
	for(int i=0;i<vec.size();i++){
		res*=10;
		res+=vec[i];
	}
	return res;
}

long long int get(long long int val,int level){
	return (val/ten[level])%10;
}

void swapp(long long int &val,int lhs,int rhs){
	int lval=get(val,lhs);
	int rval=get(val,rhs);
	val=val-(lval*ten[lhs]+rval*ten[rhs])+(lval*ten[rhs]+rval*ten[lhs]);
	return;
}

int main(){
	int n;
	cin>>n;
	vector<int> a(n);
	for(int i=0;i<n;i++){
		cin>>a[i];
		a[i]--;
	}
	vector<int> object(n);
	iota(object.begin(),object.end(),0);
	long long int ob=coder(object);
	map<long long int,int> dp[2];
	for(int loop=0;loop<2;loop++){
		long long int first;
		if(loop==0) first=coder(a);
		else first=ob;
		dp[loop][first]=0;
		queue<pair<long long int,int>> q;
		q.push(make_pair(first,0));
		while(!q.empty()){
			auto state=q.front();
			q.pop();
			if(state.second>=4) continue;
			
			//odd
			for(int i=0;i<n;i++){
				auto b=state.first;
				for(int j=0;j<n;j++){
					if(!(0<=i-j && i+j<n)) break;
					swapp(b,i-j,i+j);
					if(!dp[loop].count(b)){
						dp[loop][b]=state.second+1;
						q.push(make_pair(b,state.second+1));
					}
				}
			}
			
			//else
			for(int i=0;i<n-1;i++){
				auto b=state.first;
				for(int j=0;j<n;j++){
					if(!(0<=i-j && i+j+1<n)) break;
					swapp(b,i-j,i+j+1);
					if(!dp[loop].count(b)){
						dp[loop][b]=state.second+1;
						q.push(make_pair(b,state.second+1));
					}
				}
			}


		}
	}
	int res=9;
	for(auto it=dp[0].begin();it!=dp[0].end();it++){
		if(!(dp[1].count(it->first))) continue;
		res=min(res,(it->second)+(dp[1].find(it->first)->second));
	}
	cout<<res<<endl;
	return 0;
}