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;
#define r(i,n) for(int i=0;i<n;i++)
typedef pair<vector<int>,int>P;

int n;

map<vector<int>,int> BFS(vector<int> A){
  map<vector<int>,int>m;
  m[A]=0;
  queue<P>q;
  q.push(P(A,0));
  while(!q.empty()){
    vector<int> B=q.front().first;
    int cost=q.front().second;q.pop();
    if(cost==4)continue;
    for(int l=0;l<n-1;l++){
      for(int r=l+1;r<n;r++){
        vector<int>C=B;
        reverse(C.begin()+l,C.begin()+r+1);
        if(!m.count(C)){
          m[C]=cost+1;
          q.push(P(C,cost+1));
        }
      }
    }
  }
  return m;
}

int main(){
  cin>>n;
  vector<int>A(n),B(n);
  r(i,n)cin>>B[i],A[i]=i+1;
  map<vector<int>,int>m1=BFS(A);
  map<vector<int>,int>m2=BFS(B);
  int ans=9;
  do{
    if(m1.count(A)&&m2.count(A)){
      ans=min(ans,m1[A]+m2[A]);
    }
  }while(next_permutation(A.begin(),A.end()));
  cout<<ans<<endl;
}