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:
#define _CRT_SECURE_NO_WARNINGS
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(ll i=a; i<ll(b); i++)
#define rep(i,b) loop(i,0,b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template<class T> ostream & operator<<(ostream & os, vector<T> const &);
template<int n, class...T> typename enable_if<(n>=sizeof...(T))>::type _ot(ostream &, tuple<T...> const &){}
template<int n, class...T> typename enable_if<(n< sizeof...(T))>::type _ot(ostream & os, tuple<T...> const & t){ os << (n==0?"":" ") << get<n>(t); _ot<n+1>(os, t); }
template<class...T> ostream & operator<<(ostream & os, tuple<T...> const & t){ _ot<0>(os, t); return os; }
template<class T, class U> ostream & operator<<(ostream & os, pair<T,U> const & p){ return os << "(" << p.first << ", " << p.second << ") "; }
template<class T> ostream & operator<<(ostream & os, vector<T> const & v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; }
#ifdef DEBUG
#define dump(...) (cerr<<#__VA_ARGS__<<" = "<<mt(__VA_ARGS__)<<" ["<<__LINE__<<"]"<<endl)
#else
#define dump(...)
#endif

int N;

ll encode(int * v){
    ll res = 0;
    rep(i,N) res = res*12+v[i];
    return res;
}
void decode(ll v, int * a){
    rep(i,N){
        a[N-i-1] = v%12;
        v/=12;
    }
}

int main(){
    cin >> N;
    int init[15], goal[15];
    rep(i,N) cin >> init[i];
    rep(i,N) goal[i] = i+1;
    map<ll,int> x,y;
    ll ans = -1;
    {
        queue<pair<ll,int>> q;
        q.emplace(encode(init),0);
        x[encode(init)] = 0;
        while(q.size()){
            ll c,d;
            tie(c,d) = q.front();q.pop();
            int e[15];
            decode(c,e);
            if(x[c] < d) continue;
            if(d==4) continue;
            rep(i,N+1)rep(j,i){
                reverse(e+j,e+i);
                ll w = encode(e);
                if(x.count(w)==0){
                    q.emplace(w,d+1);
                    x[w] = d+1;
                }
                reverse(e+j,e+i);
            }
        }
    }
    {
        queue<pair<ll,int>> q;
        q.emplace(encode(goal),0);
        y[encode(goal)] = 0;
        while(q.size()){
            ll c,d;
            tie(c,d) = q.front();q.pop();
            int e[15];
            decode(c,e);
            if(y[c] < d) continue;
            if(d==4) continue;
            if(x.count(c)){
                ans = d+x[c];
                break;
            }
            rep(i,N+1)rep(j,i){
                reverse(e+j,e+i);
                ll w = encode(e);
                if(y.count(w)==0){
                    q.emplace(w,d+1);
                    y[w] = d+1;
                }
                reverse(e+j,e+i);
            }
        }
    }
    if(ans == -1) ans = 9;
    cout << ans << endl;
}