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:
//
//  3.cpp
//  practice
//
//  Created by ???????¢???? on 2017/06/11.
//  Copyright ?? 2017??´ ???????¢????. All rights reserved.
//

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
map<vector<int>,int>ma,ma2;
set<vector<int> >se,se2;
vector<int>a,b;
int n;
int main(){
    cin >> n;
    for(int i=1;i<=n;i++){
        int x; cin >> x;
        a.pb(i); b.pb(x);
    }
    queue<pair<vector<int>,int> >que;
    que.push(make_pair(a,0)); ma[a] = 0;
    while(!que.empty()){
        pair<vector<int>,int> pv = que.front(); que.pop();
        if(se.find(pv.first) != se.end()) continue;
        if(pv.second == 4) continue;
        if(pv.first == b) { printf("%d",pv.second); puts(""); return 0;}
        se.insert(pv.first);
        vector<int> v = pv.first;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                int x = i,y = j;
                while(x<y){
                    swap(v[x],v[y]);
                    x++; y--;
                }
                if(ma.find(v) == ma.end()) ma[v] = pv.second+1;
                else ma[v] = min(ma[v],pv.second+1);
                if(se.find(v) == se.end()){
                    que.push(make_pair(v,ma[v]));
                }
                x = i,y = j;
                while(x<y){
                    swap(v[x],v[y]);
                    x++; y--;
                }
            }
        }
    }
    que.push(make_pair(b,0)); ma2[b] = 0;
    while(!que.empty()){
        pair<vector<int>,int> pv = que.front(); que.pop();
        if(se2.find(pv.first) != se2.end()) continue;
        if(pv.second == 4) continue;
        se2.insert(pv.first);
        vector<int> v = pv.first;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<n;j++){
                int x = i,y = j;
                while(x<=y){
                    swap(v[x],v[y]);
                    x++; y--;
                }
                if(ma2.find(v) == ma2.end()) ma2[v] = pv.second+1;
                else ma2[v] = min(ma2[v],pv.second+1);
                if(se2.find(v) == se2.end()){
                    que.push(make_pair(v,ma2[v]));
                }
                x = i,y = j;
                while(x<=y){
                    swap(v[x],v[y]);
                    x++; y--;
                }
            }
        }
    }
    int res = 9;
    for(map<vector<int> ,int>::iterator it=ma.begin();it!=ma.end();++it){
        if(ma2.find(it->first) != ma2.end()){
            res = min(res,it->second+ma2[it->first]);
        }
    }
    cout << res << endl;
}