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;

#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

const int INF=1e9;
const int MOD=1e9+7;
const double EPS=1e-9;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
	os<<'[';
	rep(i,a.size()) os<<(i?" ":"")<<a[i];
	return os<<']';
}

ll encode(const vi& a)
{
	ll x=0;
	rep(i,a.size()) x|=ll(a[i])<<i*4;
	return x;
}
vi decode(ll x)
{
	vi a;
	for(;x;x>>=4) a.push_back(x&15);
	return a;
}

//vvi f;
//
//inline int encode(const vi& a)
//{
//	return lower_bound(all(f),a)-begin(f);
//}
//inline vi decode(int x)
//{
//	return f[x];
//}

int main()
{
	for(int n;cin>>n && n;){
		vi a(n);
		rep(i,n) cin>>a[i];
		
		vi b(n); iota(all(b),1);
		if(a==b){
			cout<<0<<endl;
			continue;
		}
		
		queue<pair<ll,int>> q[2];
		unordered_map<ll,int> m[2];
		q[0].emplace(encode(a),0); m[0].insert(mp(encode(a),0));
		q[1].emplace(encode(b),0); m[1].insert(mp(encode(b),0));
		
		int res=INF;
		for(int k=0;;k^=1){
			//dump(q[k].size());
			queue<pair<ll,int>> tmp;
			while(q[k].size()){
				ll x; int t; tie(x,t)=q[k].front(); q[k].pop();
				if(m[k^1].count(x)){
					res=t+m[k^1][x];
					goto end;
				}
				vi a=decode(x);
				rep(i,n) repi(j,i,n){
					reverse(begin(a)+i,begin(a)+j+1);
					ll x=encode(a);
					if(!m[k].count(x)){
						tmp.emplace(encode(a),t+1);
						m[k][x]=t+1;
					}
					reverse(begin(a)+i,begin(a)+j+1);
				}
			}
			swap(q[k],tmp);
		}
		end:
		cout<<res<<endl;
	}
}