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 ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

map<vi,int> mp;
map<vi,int> mq;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vi vec(n);
    rep(i,n){
        cin >> vec[i];
        --vec[i];
    }
    mp[vec] = 0;
    vector<vector<int> > st[2];
    st[0].pb(vec);
    rep(i,4){
        st[1].clear();
        rep(id,len(st[0])){
            rep(j,n-1){
                srep(k,j+1,n){
                    vi res = st[0][id];
                    for(int l=j;2*l<j+k;l++){
                        swap(res[l],res[j+k-l]);
                    }
                    if(mp.find(res) == mp.end()){
                        mp[res] = i+1;
                        st[1].pb(res);
                    }
                }
            }
        }
        swap(st[0],st[1]);
    }
    vector<vector<int> > su[2];
    vi res(n);
    iota(all(res),0);
    mq[res] = 0;
    su[0].pb(res);
    int ans = INF;
    if(mp.find(res) != mp.end()){
        cout << mp[res] << "\n";
        return 0;
    }
    rep(i,4){
        su[1].clear();
        rep(id,len(su[0])){
            rep(j,n-1){
                srep(k,j+1,n){
                    vi res = su[0][id];
                    for(int l=j;2*l<j+k;l++){
                        swap(res[l],res[j+k-l]);
                    }
                    if(mp.find(res) != mp.end()){
                        cmn(ans,mp[res]+i+1);
                    }else if(mq.find(res) == mq.end()){
                        mq[res] = i+1;
                        su[1].pb(res);
                    }
                }
            }
        }
        swap(su[0],su[1]);
    }
    if(ans == INF){
        cout << "9\n";
    }else{
        cout << ans << "\n";
    }
    return 0;
}