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<map>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;

typedef vector<int> V;
typedef pair<int,V> P;

int main(){
  int n;
  V v1,v2;
  cin >> n;
  v1.resize(n); v2.resize(n);
  for(int i=0;i<n;i++)cin >> v1[i];
  for(int i=0;i<n;i++)v2[i] = i+1;

  map<V,int> m1,m2;
  m1[v1] = 0; m2[v2] = 0;
  queue<P> q;
  q.push(P(0,v1));

  while(q.size()){
    P p = q.front(); q.pop();
    int d = p.first;
    v1 = p.second;
    if(d>3)continue;

    for(int i=0;i<n;i++){
      for(int j=i+1;j<n;j++){
	reverse(v1.begin()+i,v1.begin()+j+1);
	if(m1.find(v1) == m1.end()){
	  m1[v1] = d+1;
	  q.push(P(d+1,v1));
	}
	reverse(v1.begin()+i,v1.begin()+j+1);
      }
    }
  }

  q.push(P(0,v2));
  while(q.size()){
    P p = q.front(); q.pop();
    int d = p.first;
    v2 = p.second;
    if(d>3)continue;

    for(int i=0;i<n;i++){
      for(int j=i+1;j<n;j++){
	reverse(v2.begin()+i,v2.begin()+j+1);
	if(m2.find(v2) == m2.end()){
	  m2[v2] = d+1;
	  q.push(P(d+1,v2));
	}
	reverse(v2.begin()+i,v2.begin()+j+1);
      }
    }
  }  
  
  int m = 9;
  for(map<V,int>::iterator ite=m1.begin();ite!=m1.end();ite++){
    if(m2.find((*ite).first) != m2.end()){
      m = min(m,(*ite).second + m2[(*ite).first]);
    }
  }
  cout << m << endl;
}