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 int long long
int n;
int po[11],a[11];
void print(int x){
  for(int i=0;i<n;i++) cout<<x%11<<" ",x/=11;
  cout<<endl;
}
void sw(int &x,int i,int j){
  int s=(x%po[i+1])/po[i]*(po[j]-po[i]);
  int t=(x%po[j+1])/po[j]*(po[i]-po[j]);
  x+=s+t;
}
signed main(){
  po[0]=1;
  for(int i=0;i<10;i++) po[i+1]=po[i]*11;
  cin>>n;
  for(int i=0;i<n;i++) cin>>a[i];
  int p[2]={};
  for(int i=0;i<n;i++) p[0]=p[0]*11+a[i];
  for(int i=0;i<n;i++) p[1]=p[1]*11+(i+1);
  if(p[0]==p[1]){
    cout<<0<<endl;
    return 0;
  }
  //cout<<p<<endl<<ans<<endl;
  //print(p);print(ans);
  queue<int> q[2];
  unordered_map<int,int> m[2];
  int ans=n-1;
  for(int j=0;j<2;j++){
    q[j].push(p[j]);
    m[j][p[j]]=0;
    while(!q[j].empty()){
      int t=q[j].front();q[j].pop();
      int d=m[j][t];
      if(d>=(n-1)/2) break;
      //cout<<d<<":";print(p);
      for(int i=1;i<n-1;i++){
	int r=t;
	for(int k=1;i-k>=0&&i+k<n;k++){
	  sw(r,i-k,i+k);
	  if(!m[j].count(r)){
	    q[j].push(r);
	    m[j][r]=d+1;
	    if(j&&m[0].count(r))
	      ans=min(ans,m[0][r]+m[1][r]);
	  }
	}
      }
      for(int i=0;i<n-1;i++){
	int r=t;
	for(int k=0;i-k>=0&&i+1+k<n;k++){
	  sw(r,i-k,i+1+k);
	  if(!m[j].count(r)){
	    q[j].push(r);
	    m[j][r]=d+1;
	    if(j&&m[0].count(r))
	      ans=min(ans,m[0][r]+m[1][r]);
	  }
	}
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}