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 <algorithm>
#include <string>
#include <queue>
#include <map>
using namespace std;

typedef pair<bool,int>P;//flag,cost


int main(){
	
	
	int ans=9;
	int n;
	string s="",g,t;
	cin>>n;
	for(int i=0,j;i<n;i++){
		cin>>j;
		s+=('a'+j-1);
	}
	g=s;
	for(int i=0;i<=g.size();i++)for(int j=0;j<g.size()-1;j++)if(g[j]>g[j+1])swap(g[j],g[j+1]);
	
	queue<string>Q;
	map<string,bool>Flag;
	map<string,int>Cost;
	Q.push(s);
	Flag[ s ]=1;
	Cost[s]=0;
	
	for(int cost;!Q.empty();Q.pop()){
		s=Q.front();
		cost=Cost[s];
		
		if(cost<4){
			for(int i=0;i<g.size()-1;i++){
				for(int j=i+2;j<=g.size();j++){
					t=s;
					reverse(t.begin()+i,t.begin()+j);
					if(Flag[t]==0){
						Q.push(t);
						Flag[t]=1;
						Cost[t]=cost+1;
					}
				}
			}
		}
	}
	
	map<string,bool>Elag;
	map<string,int>Eost;
	Q.push(g);
	Elag[g]=1;
	Eost[g]=0;
	
	for(int cost;!Q.empty();Q.pop()){
		s=Q.front();
		cost=Eost[s];
		if(cost<4){
			for(int i=0;i<g.size()-1;i++){
				for(int j=i+2;j<=g.size();j++){
					t=s;
					reverse(t.begin()+i,t.begin()+j);
					if(Elag[t]==0){
						Q.push(t);
						Elag[t]=1;
						Eost[t]=cost+1;
					}
				}
			}
		}
		if(Flag[s]){
			ans=min(ans,cost+Cost[s]);
		}
	}
	
	cout<<ans<<endl;
	
	/*
	string str="abcde",r;
	for(int i=0;i<str.size()-1;i++){
		for(int j=i+2;j<=str.size();j++){
			r=str;
			reverse(r.begin()+i,r.begin()+j);
			cout<<i<<" "<<j<<" "<<r<<endl;
		}
	}
	*/
	return 0;
}