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 <iostream>
#include <vector>
#include <set>
#include <algorithm>

#define show(x) cerr << #x << " = " << x << endl;

using namespace std;
using ll = long long;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "sz:" << v.size() << "\n{";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "}\n";
    return os;
}


int main()
{
    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    vector<int> S(N);
    for (int i = 0; i < N; i++) {
        S[i] = i + 1;
    }

    constexpr int MAX = 10;
    constexpr int HALF = (MAX - 1) / 2;

    vector<set<vector<int>>> former(HALF + 1);
    vector<set<vector<int>>> latter(HALF + 1);
    former[0].insert(A);
    latter[0].insert(S);

    for (int i = 0; i < HALF; i++) {
        for (const auto& V : former[i]) {
            for (int l = 0; l < N - 1; l++) {
                for (int r = l + 1; r < N; r++) {
                    auto v = V;
                    for (int k = 0; k < (r - l + 1) / 2; k++) {
                        swap(v[l + k], v[r - k]);
                    }
                    former[i + 1].insert(v);
                }
            }
        }
        for (const auto& V : latter[i]) {
            for (int l = 0; l < N - 1; l++) {
                for (int r = l + 1; r < N; r++) {
                    auto v = V;
                    for (int k = 0; k < (r - l + 1) / 2; k++) {
                        swap(v[l + k], v[r - k]);
                    }
                    latter[i + 1].insert(v);
                }
            }
        }
    }

    //    show(former);

    for (int i = 0; i < N - 1; i++) {
        if (i <= HALF) {
            if (former[i].find(S) != former[i].end()) {
                cout << i << endl;
                return 0;
            }
        } else {
            const int res = i - HALF;
            for (const auto& v : latter[res]) {
                if (former[HALF].find(v) != former[HALF].end()) {
                    cout << i << endl;
                    return 0;
                }
            }
        }
    }
    cout << N - 1 << endl;

    return 0;
}