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;

int n;
string a;

int solve();
map<string, int> calc(string &s);

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    a += '0' + x - 1;
  }
  cout << solve() << endl;
  return 0;
}

int solve() {
  string now;
  for (int i = 0; i < n; ++i) now += '0' + i;
  map<string, int> st = calc(now), go = calc(a);
  int res = n - 1;
  for (auto p : st)
    if (go.find(p.first) != go.end()) res = min(res, p.second + go[p.first]);
  return res;
}

map<string, int> calc(string &s) {
  map<string, int> mp;
  mp[s] = 0;
  queue<string> qu;
  qu.push(s);
  while (qu.size()) {
    string now = qu.front();
    int cos = mp[now];
    if (cos >= (n - 1) / 2) break;
    qu.pop();
    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j < n; ++j) {
        string to = now;
        reverse(to.begin() + i, to.begin() + j + 1);
        if (mp.find(to) == mp.end()) {
          mp[to] = cos + 1;
          qu.push(to);
        }
      }
  }
  return mp;
}