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;
int n,ans=9;
map< vector<int> ,int > d;
map< vector<int> ,int > d2;

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

  }

  queue< vector<int> > Q;
  Q.push(a);
  d[a]=0;

  while(!Q.empty()){
    vector<int> p=Q.front();Q.pop();
    int cost=d[p];
    if(cost==4)continue;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	reverse(p.begin()+i,p.begin()+j+1);
	if( d.count(p) == 0 ){
	  d[p]=cost+1;
	  Q.push(p);
	}
	reverse(p.begin()+i,p.begin()+j+1);
      }
    }
  }

  Q.push(b);
  d2[b]=0;

  while(!Q.empty()){
    vector<int> p=Q.front();Q.pop();
    int cost=d2[p];

    if(d.count(p)>0) ans = min (ans, d[p]+cost);
    if(cost==4)continue;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	reverse(p.begin()+i,p.begin()+j+1);
	if( d2.count(p) == 0 ){
	  d2[p]=cost+1;
	  Q.push(p);
	}
	reverse(p.begin()+i,p.begin()+j+1);
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}