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;

const int INF = 1 << 30;

void calc(vector< int > &array, map< vector< int >, int > &scan)
{
  queue< vector< int > > que;
  que.emplace(array);
  while(!que.empty()) {
    auto vec = que.front();
    que.pop();
    int dep = scan[vec];
    for(int i = 0; i < array.size(); i++) {
      for(int j = i + 1; j <= array.size(); j++) {
        reverse(begin(vec) + i, begin(vec) + j);
        if(!scan.count(vec)) {
          scan[vec] = dep + 1;
          if(dep + 1 < (array.size() - 1) / 2) que.emplace(vec);
        }
        reverse(begin(vec) + i, begin(vec) + j);
      }
    }
  }
}

int main()
{
  int N;
  cin >> N;
  vector< int > A(N);
  for(int i = 0; i < N; i++) {
    cin >> A[i];
    --A[i];
  }
  vector< int > vv(N);
  iota(begin(vv), end(vv), 0);

  map< vector< int >, int > cash1, cash2;
  calc(A, cash1);
  calc(vv, cash2);

  int ret = N - 1;
  do {
    if(cash1.count(vv) && cash2.count(vv))
      ret = min(ret, cash1[vv] + cash2[vv]);
  } while(next_permutation(begin(vv), end(vv)));

  cout << ret << endl;
}