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 10
#define INF 1e9
typedef vector<int> Vec;
 
int N, res;
map<Vec,int> dist[2];
Vec v[2];
 
void bfs(int p){
    int t = !p;
    Vec next;
    queue<Vec> que; 
 
    dist[p][v[p]] = 0;
    que.push(v[p]);
 
    while(!que.empty()){
	Vec s = que.front(); que.pop();
 
	if(s == v[t]){
	    res = dist[p][s];
	    break;
	}
 
	if(dist[p][s] >= (N-1)/2){
	    break;
	}
 
	for(int i = 0 ; i < N-1 ; i++){
	    for(int j = i + 1 ; j < N ; j++){
		next = s;
		reverse(next.begin()+i, next.begin()+j+1);
 
		if(!dist[p].count(next)){
		    dist[p][next] = dist[p][s] + 1;
		    que.push(next);
		} 
	    }
	}
    }
}
 
int main(){
    cin >> N;
    v[0].resize(N);
    for(int i = 0 ; i < N ; i++){
	cin >> v[0][i];
	v[1].push_back(i+1);
    }
 
    res = 9;
    bfs(0); bfs(1);
    map<Vec,int>::iterator it, val;
   
    for(it = dist[0].begin() ; it != dist[0].end() ; ++it){
	val = dist[1].find(it->first);
	if(val != dist[1].end()){
	    res = min(res, it->second+val->second);
	}    
    }
    cout << res << endl;
 
    return 0;
}