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"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"


int main() {
	int N; cin >> N;
	string starts;
	for (int i = 0; i < N; ++i) {
		int a; cin >> a;
		a--;
		starts.push_back(a + '0');
	}
	string goals; 
	for (int i = 0; i < N; ++i) {
		goals.push_back(i + '0');
	}
	vector < vector<string>>fromstarts(5);
	fromstarts[0].emplace_back(starts);
	vector< vector<string>>fromgoals(5);
	fromgoals[0].emplace_back(goals);
	for (int i = 0; i < 4; ++i) {
		vector<string>aset;
		for (auto v : fromstarts[i]) {
			for (int l = 0; l < N; ++l) {
				for (int r = l+2; r <= N; ++r) {
					string rev = v.substr(l, r - l);
					reverse(rev.begin(), rev.end());
					string next = v.substr(0, l) + rev + (v.substr(r, N - r));
					aset.emplace_back(next);
				}
			}
		}
		sort(aset.begin(), aset.end());
		aset.erase(unique(aset.begin(), aset.end()), aset.end());
		fromstarts[i + 1] = aset;
	}
	for (int i = 0; i < 4; ++i) {
		vector<string>aset;
		for (auto v : fromgoals[i]) {
			for (int l = 0; l < N; ++l) {
				for (int r = l + 2; r <= N; ++r) {
					string rev = v.substr(l, r - l);
					reverse(rev.begin(), rev.end());
					string next = v.substr(0, l) + rev + (v.substr(r, N - r));
					aset.emplace_back(next);
				}
			}
		}
		sort(aset.begin(), aset.end());
		aset.erase(unique(aset.begin(), aset.end()), aset.end());
		fromgoals[i + 1] = aset;
	}
	int ans = 9;
	for (int s = 0; s < 4; ++s) {
		for (int t = 0; t < 4; ++t) {
			vector<string>sames;
			set_intersection(fromstarts[s].begin(), fromstarts[s].end(), fromgoals[t].begin(), fromgoals[t].end(), inserter(sames, sames.end()));
			if (!sames.empty()) {
				ans = min(ans, s+t);
			}
		}
	}
	cout << ans << endl;
	return 0;
}