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<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int n, a[10];

int main(){
	cin >> n;
	rep(i, n) cin >> a[i];
	
	map<ll, int> m;
	set<ll> s;
	queue<pair<int, ll> > q;
	ll t = 0, g = 0;
	rep(i, n){
		t *= 16, t += a[i];
		g *= 16, g += i + 1;
	}
	q.push(mp(0, g));
	m[g] = 0;
	
	while(!q.empty()){
		ll t = q.front().second;
		int c = q.front().first; q.pop();
		
		rep(i, n) a[n - 1 - i] = t % 16, t /= 16;
		rep(i, n) for(int j = i + 1; j < n; j++){
			reverse(a + i, a + j + 1);
			ll h = 0;
			rep(k, n) h *= 16, h += a[k];
			if(!m.count(h)){
				m[h] = c + 1;
				if(c < 3) q.push(mp(c + 1, h));
			}
			reverse(a + i, a + j + 1);
		}
	}
	if(m.count(t)){
		cout << m[t] << endl;
		return 0;
	}
	q.push(mp(0, t));
	s.insert(t);
	
	while(!q.empty()){
		ll t = q.front().second;
		int c = q.front().first; q.pop();
		
		rep(i, n) a[n - 1 - i] = t % 16, t /= 16;
		rep(i, n) for(int j = i + 1; j < n; j++){
			reverse(a + i, a + j + 1);
			ll h = 0;
			rep(k, n) h *= 16, h += a[k];
			if(m.count(h)){
				cout << c + 1 + m[h] << endl;
				return 0;
			}
			if(c < 3 && !s.count(h)){
				s.insert(h);
				q.push(mp(c + 1, h));
			}
			reverse(a + i, a + j + 1);
		}
	}
	cout << 9 << endl;
	
	return 0;
}