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>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<vector<vector<char> >,int> P;
const long long int MOD = 1000000007;
const int INF = 1000000000;

int n;
int num[11], tmp[11];
unordered_map<ll, int> m;
unordered_map<ll, int> mm;
queue<ll> que;

int main(){
    cin >> n;
    ll st = 0;
    rep(i,n){
        int x;
        cin >> x;
        if(x == 10){
            st = st*100+x;
        } else{
            st = st*10+x;
        }
    }
    que.push(st);
    m[st] = 0;
    while(!que.empty()){
        ll p = que.front();
        ll q = p;
        que.pop();
        int now = 0;
        while(p){
            if(p%10 == 0){
                num[n-now] = 10;
                p /= 100;
            } else{
                num[n-now] = p%10;
                p /= 10;
            }
            now++;
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++) tmp[j] = num[j];
            for(int j = 1; j <= n; j++){
                if(i-j <= 0 || i+j > n) break;
                swap(tmp[i-j],tmp[i+j]);
                ll add = 0;
                for(int k = 1; k <= n; k++){
                    if(tmp[k] == 10){
                        add = add*100+10;
                    } else{
                        add = add*10+tmp[k];
                    }
                }
                if(m.count(add)) continue;
                m[add] = m[q]+1;
                if(m[add] < 5) que.push(add);
            }
        }
        for(int i = 1; i < n; i++){
            int ii = i+1;
            for(int j = 1; j <= n; j++) tmp[j] = num[j];
            for(int j = 0; j <= n; j++){
                if(i-j <= 0 || ii+j > n) break;
                swap(tmp[i-j],tmp[ii+j]);
                ll add = 0;
                for(int k = 1; k <= n; k++){
                    if(tmp[k] == 10){
                        add = add*100+10;
                    } else{
                        add = add*10+tmp[k];
                    }
                }
                if(m.count(add)) continue;
                m[add] = m[q]+1;
                if(m[add] < 5) que.push(add);
            }
        }
    }
    while(!que.empty()) que.pop();
    ll en = 0;
    for(int i = 1; i <= n; i++){
        if(i == 10){
            en = en*100+i;
        } else{
            en = en*10+i;
        }
    }
    que.push(en);
    if(m.count(en)){
        cout << m[en] << endl;
        return 0;
    }
    mm[en] = 0;
    while(!que.empty()){
        ll p = que.front();
        ll q = p;
        if(m.count(q)){
            cout << m[q]+mm[q] << endl;
            return 0;
        }
        que.pop();
        int now = 0;
        while(p){
            if(p%10 == 0){
                num[n-now] = 10;
                p /= 100;
            } else{
                num[n-now] = p%10;
                p /= 10;
            }
            now++;
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++) tmp[j] = num[j];
            for(int j = 1; j <= n; j++){
                if(i-j <= 0 || i+j > n) break;
                swap(tmp[i-j],tmp[i+j]);
                ll add = 0;
                for(int k = 1; k <= n; k++){
                    if(tmp[k] == 10){
                        add = add*100+10;
                    } else{
                        add = add*10+tmp[k];
                    }
                }
                if(mm.count(add)) continue;
                mm[add] = mm[q]+1;
                if(mm[add] < 5) que.push(add);
            }
        }
        for(int i = 1; i < n; i++){
            int ii = i+1;
            for(int j = 1; j <= n; j++) tmp[j] = num[j];
            for(int j = 0; j <= n; j++){
                if(i-j <= 0 || ii+j > n) break;
                swap(tmp[i-j],tmp[ii+j]);
                ll add = 0;
                for(int k = 1; k <= n; k++){
                    if(tmp[k] == 10){
                        add = add*100+10;
                    } else{
                        add = add*10+tmp[k];
                    }
                }
                if(mm.count(add)) continue;
                mm[add] = mm[q]+1;
                if(mm[add] < 5) que.push(add);
            }
        }
    }
}