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

string rev(const string &s, int i, int j){
  string ret = s;
  for(int k = i; k <= j; ++k) ret[k] = s[j-k+i];
  return ret;
}

int main(){
  int N;
  cin >> N;
  string A(N,'0');
  for(int i = 0; i < N; ++i){
    int t;
    cin >> t;
    A[i] = '0' + t - 1;
  }
  string C(N, '0');
  for(int i = 0; i < N; ++i) C[i] = '0' + i;
  if(A == C){
    cout << 0 << endl;
    return 0;
  }
  queue<string> que;
  que.push(A);
  map<string, int> M;
  M[A] = 1;
  while(!que.empty()){
    string s = que.front();
    que.pop();
    for(int i = 0; i < N; ++i){
      for(int j = i+1; j < N; ++j){
        string t = rev(s, i, j);
        if(M[t] > 0) continue;
        M[t] = M[s] + 1;
        if(t == C){
          cout << M[t]-1 << endl;
          return 0;
        }
        if(M[t] < N/2){
          que.push(t);
        }
      }
    }
  }
  map<string, int> M_;
  M_[C] = 1;
  que.push(C);
  while(!que.empty()){
    string s = que.front();
    que.pop();
    for(int i = 0; i < N; ++i){
      for(int j = i+1; j < N; ++j){
        string t = rev(s, i, j);
        if(M_[t] > 0) continue;
        M_[t] = M_[s] + 1;
        if(M[t] > 0){
          cout << M[t] + M_[t] - 2 << endl;
          return 0;
        }
        if(M_[t] <= N/2) que.push(t);
      }
    }
  }
  cout << N-1 << endl;
  return 0;
}