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 int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int H(vector<int>&a){
    int ret=0,f=1;
    for(int i=a.size()-1;i>=0;i--){
        int cnt=0;
        for(int j=i;j<a.size();j++)if(a[i]>a[j])cnt++;
        ret+=f*cnt;
        f*=a.size()-i;
    }
    return ret;
}

void bfs(vint A,vint &cost){
    fill(all(cost),1001001001);
    cost[H(A)]=0;
    queue<vint>que;
    que.push(A);
    while(que.size()){
        vint a=que.front();
        que.pop();
        int h0=H(a);
        if(cost[h0]==4)break;
        for(int i=0;i<a.size();i++){
            for(int j=i+1;j<=a.size();j++){
                reverse(a.begin()+i,a.begin()+j);
                int h=H(a);
                if(cost[h]==1001001001){
                    cost[h]=cost[h0]+1;
                    que.push(a);
                }
                reverse(a.begin()+i,a.begin()+j);
            }
        }
    }
}

signed main(){
    int N;cin>>N;
    vector<int>A(N);
    rep(i,N)cin>>A[i];

    vector<int>B(N);
    rep(i,N)B[i]=i+1;

    vector<int>costA(3628800),costB(3628800);
    bfs(A,costA);bfs(B,costB);

    int ans=9;
    rep(i,3628800)chmin(ans,costA[i]+costB[i]);
    cout<<ans<<endl;
    return 0;
}