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<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define mp1(a,b,c) P1(a,P(b,c))

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

int n;
struct A{
	int a[10];
	bool operator<(A a_) const {
		rep(i,n){
			if(a_.a[i] < this->a[i])return true;
			if(a_.a[i] > this->a[i])return false;
		}
		return false;
	}
	bool operator==(A a_) const {
		rep(i,n){
			if(a_.a[i] != this->a[i])return false;
		}
		return true;
	}
};

set<pair<A,int>> S;

A R(A x,int i,int j){
	A ret = x;
	for(int k = i ; k <= j ; k ++){
		ret.a[k] = x.a[i+j-k];
	}
	return ret;
}

int main(){
	scanf("%d",&n);
	
	A a; rep(i,n)a.a[i] = i;
	S.insert(pair<A,int>(a,0));
	rep(ppp,4){
		for(set<pair<A,int>>::iterator itr = S.begin() ; itr != S.end() ; itr ++){
			if(itr->sc != ppp)continue;
			rep(i,n){
				for(int j = i+1 ; j < n ; j ++){
					a = R(itr->fr,i,j);
					if(!(S.lower_bound(pair<A,int>(a,0))->fr == a)){
						S.insert(pair<A,int>(a,ppp+1));
					}
				}
			}
		}
		//cout << ppp << " " << S.size() << endl;
	}
	
	rep(i,n){
		int aa;
		scanf("%d",&aa);
		a.a[i] = aa-1;
	}
	
	int ret = n-1;
	for(set<pair<A,int>>::iterator itr = S.begin() ; itr != S.end() ; itr ++){
		A b;
		rep(i,n)b.a[i] = a.a[itr->fr.a[i]];
		set<pair<A,int>>::iterator it = S.lower_bound(pair<A,int>(b,0));
		if(it->fr == b)ret = min ( ret , itr->sc + it->sc );
	}
	cout << ret << endl;
}