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 <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
int n,a[10],inf=1e8;
typedef vector<int> vi;
typedef pair<int,vi> P;
map<vi,int> d,d2;
void bfs(vi s){
	d[s]=0;
	priority_queue< P,vector<P>,greater<P> > que;
	que.push(P(0,s));
	while(!que.empty()){
		P p=que.top();
		que.pop();
		vi v=p.sc;
		if(d[v]==4) break;
		rep(i,n) rep(j,i){
			vi vc=v;
			reverse(vc.begin()+j,vc.begin()+i+1);
			if(d.count(vc)) continue;
			d[vc]=d[v]+1;
			que.push(P(d[vc],vc));
		}
	}
}
int bfs2(vi s){
	d2[s]=0;
	priority_queue< P,vector<P>,greater<P> > que;
	que.push(P(0,s));
	while(!que.empty()){
		P p=que.top();
		que.pop();
		vi v=p.sc;
		if(d.count(v)) return d[v]+d2[v];
		if(d2[v]==4) return 9;
		rep(i,n) rep(j,i){
			vi vc=v;
			reverse(vc.begin()+j,vc.begin()+i+1);
			if(d2.count(vc)) continue;
			d2[vc]=d2[v]+1;
			que.push(P(d2[vc],vc));
		}
	}
}

int main(){
	cin>>n;
	rep(i,n) cin>>a[i];
	vi s,t;
	rep(i,n) s.pb(i+1);
	rep(i,n) t.pb(a[i]);
	bfs(s);
	cout<<bfs2(t)<<endl;
}