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 rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf=1e9;
const int64_t inf64=1e18;
const double eps=1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}

void solve(){
    int n;
    cin >> n;
    int64_t s=0;
    rep(i,0,n){
        int64_t a;
        cin >> a;
        --a;
        s=s*10+a;
    }

    int64_t sorted=0;
    rep(i,0,n) sorted=sorted*10+i;

    int64_t exp[10];
    exp[0]=1;
    rep(i,1,10) exp[i]=exp[i-1]*10;
    auto reverse=[&](int64_t x,int l,int r){
        rep(i,0,(r-l+1)/2){
            int64_t p=x/exp[l+i]%10,q=x/exp[r-i]%10;
            x-=p*exp[l+i]+q*exp[r-i];
            x+=q*exp[l+i]+p*exp[r-i];
        }
        return x;
    };

    unordered_map<int64_t,int> num1;
    {
        queue<int64_t> queues[5];
        num1[s]=0;
        queues[0].push(s);
        rep(k,0,5){
            auto &que=queues[k];
            while(!que.empty()){
                auto x=que.front();
                que.pop();
                if(x==sorted){
                    cout << k << endl;
                    return;
                }
                int l,r;
                rep(i,0,n){
                    if(x/exp[i]%10!=n-i-1){
                        l=i;
                        break;
                    }
                }
                for(int i=n-1; i>=0; --i){
                    if(x/exp[i]%10!=n-i-1){
                        r=i;
                        break;
                    }
                }
                rep(i,l,r){
                    rep(j,i+1,r+1){
                        int64_t y=reverse(x,i,j);
                        if(num1.find(y)!=num1.end()) continue;
                        if(y==sorted){
                            cout << k+1 << endl;
                            return;
                        }
                        num1[y]=k+1;
                        if(k+1<5) queues[k+1].push(y);
                    }
                }
            }
        }
    }

    unordered_map<int64_t,int> num2;
    {
        queue<int64_t> queues[5];
        num2[sorted]=0;
        queues[0].push(sorted);
        rep(k,0,5){
            auto &que=queues[k];
            while(!que.empty()){
                auto x=que.front();
                que.pop();
                rep(i,0,n){
                    rep(j,i+1,n){
                        int64_t y=reverse(x,i,j);
                        if(num2.find(y)!=num2.end()) continue;
                        if(num1.find(y)!=num1.end()){
                            cout << num1[y]+k+1 << endl;
                            return;
                        }
                        num2[y]=k+1;
                        if(k+1<5) queues[k+1].push(y);
                    }
                }
            }
        }
    }
    cout << 10 << endl;
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(10);
    solve();
    return 0;
}