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 <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)

const long long mod[] = {999999937LL, 1000000007LL}, base = 9973;

map<VI,int> mp;

void Rev(int l,int r,vector<int> &v){
  for(int i=0;i<=10;i++){
    if(l+i>=r-i)break;
    swap(v[l+i],v[r-i]);
  }
  return;
}

void bfs(vector<int> v,int depth){
  if(depth>=4)return;
  for(int i=0;i<v.size();i++){
    for(int j=i+1;j<v.size();j++){
      vector<int>v1;
      v1 = v;
      Rev(i,j,v1);
      if(mp[v1]!=0){
        if(mp[v1]>depth){
          mp[v1] = depth;
          bfs(v1,depth+1);
        }
      }else if(mp[v1]==0){
        mp[v1] = depth;
        bfs(v1,depth+1);
      }
    }
  }
  return;
}

void bfs2(vector<int> v,int depth,int &ans){
  if(depth>=4)return;
  for(int i=0;i<v.size();i++){
    for(int j=i+1;j<v.size();j++){
      vector<int>v1;
      v1 = v;
      Rev(i,j,v1);
      if(mp[v1]!=0){
        ans = min(ans,mp[v1]+depth);

      }else if(mp[v1]==0){
        bfs2(v1,depth+1,ans);
      }
    }
  }
  return;
}



int main(){
  int n;
  cin >>n;
  VI a(n);
  rep(i,n)cin>>a[i];
  mp[a]=0;
  bfs(a,1);
  VI b(n);
  rep(i,n)b[i]=i+1;
  if(a==b){
    cout << 0 << endl;
    return 0;
  }
  int ans = 10000;
  if(mp[b]!=0){
    ans = mp[b];
  }
  bfs2(b,1,ans);
  if(ans==10000){
    cout << n-1 << endl;
  }else{
    cout << ans << endl;
  }
  return 0;
}