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 <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    int n;
    cin >> n;
    string init(n, ' ');
    for(int i=0; i<n; ++i){
        int a;
        cin >> a;
        init[i] = '0' + a - 1;
    }

    string goal(n, ' ');
    for(int i=0; i<n; ++i)
        goal[i] = '0' + i;
    if(init == goal){
        cout << 0 << endl;
        return 0;
    }

    vector<set<string> > history(2);
    history[0].insert(init);
    history[1].insert(goal);
    vector<queue<string> > q(2);
    q[0].push(init);
    q[1].push(goal);

    int ret = 1;
    for(int k=0; ; k=(k+1)%2){
        int m = q[k].size();
        while(--m >= 0){
            string s = q[k].front();
            q[k].pop();

            for(int i=0; i<n; ++i){
                for(int j=i+1; j<n; ++j){
                    string t = s;
                    reverse(t.begin()+i, t.begin()+j+1);

                    if(history[k^1].find(t) != history[k^1].end()){
                        cout << ret << endl;
                        return 0;
                    }

                    if(history[k].find(t) == history[k].end()){
                        history[k].insert(t);
                        q[k].push(t);
                    }
                }
            }
        }
        ++ ret;
    }
}