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;
using vi = vector<int>;

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	int N;
	cin >> N;
	vi a(N);
	for (int i = 0; i < N; i++) {
		cin >> a[i]; --a[i];
	}
	vi b = a;
	sort(b.begin(), b.end());
	if (a == b) {
		puts("0");
		return 0;
	}
	queue<vi> q[2];
	map<vi, int> d[2];
	q[0].push(a);
	d[0][a] = 0;
	q[1].push(b);
	d[1][b] = 0;
	int dist = 0;
	while (++dist) {
		for (int i = 0; i < 2; i++) {
			while (!q[i].empty() && d[i][q[i].front()] < dist) {
				auto v = q[i].front(); q[i].pop();
				for (int x = 0; x < N; x++) {
					for (int y = x + 2; y <= N; y++) {
						auto tmp = v;
						reverse(tmp.begin() + x, tmp.begin() + y);
						if (d[i].count(tmp) == 0) {
							q[i].push(tmp);
							d[i][tmp] = d[i][v] + 1;
							if (d[!i].count(tmp)) {
								cout << d[0][tmp] + d[1][tmp] << endl;
								return 0;
							}
						}
					}
				}
			}
		}
	}
	assert(false);
	return 0;
}