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;
typedef pair<vector<int>,int> P;
int n,ans=9;
vector<int> v[2];
map<vector<int>,int> s[2];

void bfs(bool f){
  queue<P> Q;
  Q.push(P(v[f],0));
  while(!Q.empty()){
    P t=Q.front();Q.pop();
    vector<int> b=t.first;
    int d=t.second;
    if(f&&s[0].count(b))ans=min(ans,s[0][b]+d);
    if(s[f].count(b))continue;
    s[f][b]=d;
    if(d==4)continue;
    for(int i=0;i<n;i++)
      for(int j=i+2;j<=n;j++){
	reverse(b.begin()+i,b.begin()+j);
	Q.push(P(b,d+1));
	reverse(b.begin()+i,b.begin()+j);
      }
  }
}

int main(){
  cin>>n;
  for(int i=1,a;i<=n;i++){
    cin>>a;
    v[0].push_back(a);
    v[1].push_back(i);
  }
  bfs(0),bfs(1);
  cout<<ans<<endl;
  return 0;
}