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;

typedef vector<int> vi;

long long f(vi a){
	long long x = 0;
	for(int i = 0 ; i < a.size() ; i++) x = x * 10 + a[i];
	return x;
}
int n;
inline vi g(long long t){
	vi v(n);
	for(int i = 0 ; i < n ; i++){
		v[n-i-1] = t % 10;
		t /= 10;
	}
	return v;
}

map<long long,int> F(vi init,vi goal){
	queue<long long> Q;
	map<long long,int> done;
	done[f(init)] = 0;
	Q.push(f(init));
	while( Q.size() ){
		auto q_ = Q.front(); Q.pop();
		auto q = g(q_);
		if( done[q_] >= 4) break;
		for(int i = 0 ; i < n ; i++){
			for(int j = i + 1 ; j < n ; j++){
				vi v = q;
				int l = j - i + 1;
				for(int k = 0 ; k < l/2 ; k++){
					swap(v[i+k],v[j-k]);
				}
				long long h = f(v);
				if( done.count(h) == 0 ){
					Q.push(h);
					done[h] = done[q_] + 1;
				}
			}
		}
	}
	return done;
}
int main(){
	cin >> n;
	vi init(n);
	vi goal(n);
	for(int i = 0 ; i < n ; i++){
		cin >> init[i];
		goal[i] = i;
		init[i]--;
	}
	auto A = F(init,goal);
	auto B = F(goal,init);

	if( A.count(f(goal)) ){
		cout << A[f(goal)] << endl;
	}else{
		int ans = 9;
		for( auto x : A){
			if( B.count(x.first) ){
				ans = min(ans,x.second+B[x.first]);
			}
		}
		cout << ans << endl;
	}


}