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<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-4;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
map<ll, bool> mp,cmp; int n;
ll calc(vector<int> v) {
	ll res = 0;
	rep(i, n) {
		res *= 10; res += v[i];
	}
	return res;
}
ll rev(ll x, int l, int r) {
	vector<int> v;
	rep(i, n) {
		v.push_back((int)(x % 10)); x /= 10;
	}
	reverse(v.begin(), v.end());
	reverse(v.begin() + l, v.begin() + r + 1);
	ll res = 0;
	rep(i, n) {
		res *= 10; res += v[i];
	}
	return res;
}
int main() {
	cin >> n;
	ll sta = 0; ll g = 0;
	rep(i, n) {
		int a; cin >> a; a--; g *= 10; g += a;
		sta *= 10; sta += i;
	}
	if (sta == g) {
		cout << 0 << endl; return 0;
	}
	queue<ll> q; q.push(sta); mp[sta] = true;
	queue<ll> p; p.push(g); cmp[g] = true;
	int c = 0; int sc = 0;
	bool fin = false;
	while (!fin) {
		int len = q.size();
		rep(j, len) {
			ll x = q.front(); q.pop();
			rep(i, n) {
				Rep(k, i + 1, n) {
					ll val = rev(x, i, k);
					if (cmp[val]) {
						cout << c + sc + 1 << endl; return 0;
					}
					if (!mp[val]) {
						mp[val] = true; q.push(val);
					}
				}
			}
		}
		c++;
		len = p.size();
		rep(j, len) {
			ll x = p.front(); p.pop();
			rep(i, n) {
				Rep(k, i + 1, n) {
					ll val = rev(x, i, k);
					if (mp[val]) {
						cout << c + sc + 1 << endl; return 0;
					}
					if (!cmp[val]) {
						cmp[val] = true; p.push(val);
					}
				}
			}
		}
		sc++;
	}
	//stop
	return 0;
}