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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,int> T;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
int N;
int A[12];
int ans;
queue<T> que[2];
map<ll,int> ma[2];
ll encode(vector<int> vec)
{
	ll res = 0ll;
	for(int i=0;i<N;i++)res = (res<<4)+vec[i];
	return res;
}
vector<int> decode(ll res)
{
	vector<int> v;
	for(int i=0;i<N;i++)
	{
		v.pb(((1<<4)-1)&res);
		res >>= 4;
	}
	reverse(all(v));
	return v;
}
void bfs(int f,ll start)
{
	queue<T> &q = que[f];
	map<ll,int> &m = ma[f];
	q.push(T(start,0));
	m[start]=0;
	while(!q.empty())
	{
		T a = q.front();
		q.pop();
		vector<int> v = decode(a.fi);
		vector<int> tmp = v;
		if(a.sec>=(N-1)/2)return;
		for(int i=0;i<N;i++)
		{
			for(int j=i+1;j<N;j++)
			{
				for(int k=i;k<=j;k++)tmp[j-(k-i)]=v[k];
				ll e = encode(tmp);
				if(m.find(e)==m.end())
				{
					m[e]=a.sec+1;
					q.push(T(e,a.sec+1));
				}
				for(int k=i;k<=j;k++)tmp[k]=v[k];
			}
		}
	}
}
int main()
{
	scanf("%d",&N);
	for(int i=0;i<N;i++)scanf("%d",&A[i]);
	vector<int> vec;
	for(int i=0;i<N;i++)vec.pb(A[i]);
	bfs(0,encode(vec));
	for(int i=0;i<N;i++)vec[i]=i+1;
	bfs(1,encode(vec));
	ans = N-1;
	map<ll,int>::iterator it1,it2;
	it1=ma[0].begin();it2=ma[1].begin();
	while(1)
	{
		if(it1==ma[0].end()||it2==ma[1].end())break;
		else if(it1->fi < it2->fi)it1++;
		else if(it1->fi > it2->fi)it2++;
		else
		{
			ans = min(ans,it1->sec+it2->sec);
			it1++;
			it2++;
		}
	}
	printf("%d\n",ans);
	return 0;
}