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;

void bfs(vector<int> v, map<vector<int>, int>& m) {
    const int N = v.size();
    queue<vector<int>> que;
    que.push(v);
    m[v] = 0;
    while(!que.empty()) {
        auto now = que.front();
        que.pop();
        int cost = m[now];
        if(cost >= (N-1)/2) {
            continue;
        }
        for(int i=0; i<N; ++i) {
            for(int j=i+2; j<=N; ++j) {
                auto next = now;
                reverse(next.begin()+i, next.begin()+j);
                if(m.count(next) == 0) {
                    m[next] = cost + 1;
                    que.push(next);
                }
            }
        }
    }
}

int main() {
    int N;
    cin >> N;
    vector<int> v(N);
    for(int i=0; i<N; ++i) {
        cin >> v[i];
    }
    auto v2 = v;
    sort(v2.begin(), v2.end());
    map<vector<int>, int> m1, m2;
    int res = N - 1;
    bfs(v, m1);
    bfs(v2, m2);
    do {
        if(m1.count(v2) == 0 || m2.count(v2) == 0) {
            continue;
        }
        res = min(res, m1[v2] + m2[v2]);
    } while(next_permutation(v2.begin(), v2.end()));
    cout << res << endl;
}