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<string>
#include<functional>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

#define DBG cerr << '!' << endl;
#define REP(i,n) for(int (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(int (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(int (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int i = 0;i < (n);i++)cerr << v[i] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

map<string,int> mp;

int main()
{
	int n;cin >>n;
	string str,t;
	REP(i,n)t += (i+'0');
	mp[t] = 0;
	
	queue< pair<string,int> > q;
	q.push(MP(t,0));
	
	while(!q.empty())
	{
		pair<string,int> tmp = q.front();q.pop();
		for(int i = 0;i < tmp.FI.size()-1;i++)
		{
			for(int j = i+1;j < tmp.FI.size();j++)
			{
				string tt = tmp.FI;
				reverse(tt.begin()+i,tt.begin()+j+1);
				if(mp.find(tt) == mp.end())
				{
					mp[tt] = tmp.SE+1;
					if(tmp.SE+1 < 4)
					{
						q.push(MP(tt,tmp.SE+1));
					}
				}
			}
		}
	}
	
	REP(i,n)
	{
		int c;cin >> c;
		str += c+'0'-1;
	}
	
	if(mp.find(str) != mp.end())
	{
		cout << mp[str] << endl;
		return 0;
	}
		
	q.push(MP(str,0));
	
	
	while(!q.empty())
	{
		pair<string,int> tmp = q.front();q.pop();
		for(int i = 0;i < tmp.FI.size()-1;i++)
		{
			for(int j = i+1;j < tmp.FI.size();j++)
			{
				string tt = tmp.FI;
				reverse(tt.begin()+i,tt.begin()+j+1);
				if(mp.find(tt) != mp.end())
				{
					if(mp[tt] != -1)
					{
						cout << mp[tt] + tmp.SE + 1<< endl;
						return 0;
					}
				}
				else
				{
					mp[tt] = -1;
					if(tmp.SE+1 < 4)
						q.push(MP(tt,tmp.SE+1));
				}
			}
		}
	}
	
	cout << n-1 << endl;
	return 0;
}