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 <queue>
#include <map>
#include <algorithm>
using namespace std;

int N;

long long encode(const int *a)
{
  long long n = 0;
  for (int i = 0; i < N; i++) {
    n = 10*n + a[i];
  }
  return n;
}

void decode(int *a, long long n)
{
  for (int i = 0; i < N; i++) {
    a[N-i-1] = n % 10;
    n /= 10;
  }
}

void bfs(map<long long, int>& dist, long long start)
{
  queue<pair<long long, int> > q;
  dist[start] = 0;
  q.push(make_pair(start, 0));
  while (!q.empty()) {
    int a[10];
    decode(a, q.front().first);
    const int d = q.front().second;
    q.pop();
    for (int i = 0; i < N; i++) {
      for (int j = i+1; j < N; j++) {
        int b[10];
        copy(a, a+N, b);
        reverse(b+i, b+j+1);
        const long long n = encode(b);
        const map<long long, int>::const_iterator it = dist.find(n);
        if (it == dist.end()) {
          dist.insert(make_pair(n, d+1));
          if (d+1 < 4) {
            q.push(make_pair(n, d+1));
          }
        }
      }
    }
  }
}

int main()
{
  cin >> N;
  int a[10], b[10];
  for (int i = 0; i < N; i++) {
    cin >> a[i];
    --a[i];
    b[i] = i;
  }
  map<long long, int> m1, m2;
  bfs(m1, encode(a));
  bfs(m2, encode(b));
  int ans = 9;
  for (map<long long, int>::const_iterator it = m1.begin(); it != m1.end(); ++it) {
    map<long long, int>::const_iterator jt = m2.find(it->first);
    if (jt != m2.end()) {
      ans = min(ans, it->second + jt->second);
    }
  }
  cout << ans << endl;
  return 0;
}