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 max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000000000000

typedef long long LL;
typedef long double LD;

int main(){
  int n;
  cin >> n;

  vector<int> fact(n+1);
  fact[0]=1;
  for(int i=1;i<=n;i++) fact[i]=fact[i-1]*i;

  vector<int> diff(1<<n,0),num(1<<n,0);
  for(int i=0;i<(1<<n);i++){
    for(int j=0;j<n;j++){
      if(i&(1<<j)){
        diff[i]+=fact[n-j-1];
        num[i]++;
      }
    }
  }

  vector<int> a(n),b(n);
  for(int i=0;i<n;i++) cin >> a[i];

  int start=0;
  int count;
  for(int i=1;i<n;i++){
    count=0;
    for(int j=0;j<n;j++){
      if(a[j]==i){
        start+=count*fact[n-i];
        break;
      }else if(a[j]>i){
        count++;
      }
    }
  }

  vector<int> dist(fact[n],-1);
  dist[start]=0;
  queue<int> bfs;
  bfs.push(start);
  int now,next,nt,dbg;
  while(!bfs.empty()){
    now=nt=bfs.front();
    bfs.pop();
    for(int i=0;i<n;i++) a[i]=-1;
    for(int i=0;i<n;i++){
      count=nt/fact[n-i-1];
      nt-=count*fact[n-i-1];
      for(int j=0;j<n;j++){
        if(a[j]<0){
          if(count==0){
            a[j]=i;
            break;
          }
          count--;
        }
      }
    }
    for(int i=1;i<n-1;i++){
      count=(1<<a[i]);
      next=now;
      for(int j=1;j<=min(i,n-i-1);j++){
        count+=(1<<a[i+j])+(1<<a[i-j]);
        next+=num[count&((1<<n)-(1<<(a[i-j]+1)))]*fact[n-a[i-j]-1]-num[count&((1<<n)-(1<<(a[i+j]+1)))]*fact[n-a[i+j]-1];
        if(a[i+j]>a[i-j]){
          next+=diff[count&((1<<a[i+j])-(1<<(a[i-j]+1)))];
        }else{
          next-=diff[count&((1<<a[i-j])-(1<<(a[i+j]+1)))];
        }
        if(dist[next]==-1){
          dist[next]=dist[now]+1;
          bfs.push(next);
        }
      }
    }
    for(int i=1;i<n;i++){
      count=0;
      next=now;
      for(int j=1;j<=min(i,n-i);j++){
        count+=(1<<a[i+j-1])+(1<<a[i-j]);
        next+=num[count&((1<<n)-(1<<(a[i-j]+1)))]*fact[n-a[i-j]-1]-num[count&((1<<n)-(1<<(a[i+j-1]+1)))]*fact[n-a[i+j-1]-1];
        if(a[i+j-1]>a[i-j]){
          next+=diff[count&((1<<a[i+j-1])-(1<<(a[i-j]+1)))];
        }else{
          next-=diff[count&((1<<a[i-j])-(1<<(a[i+j-1]+1)))];
        }
        if(dist[next]==-1){
          dist[next]=dist[now]+1;
          bfs.push(next);
        }
      }
    }
    if(dist[0]!=-1){
      cout << dist[0] << endl;
      return 0;
    }
  }
  return 0;
}