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 <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64, i64> P;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

void rec(vector<int> &x, map<vector<int>, int> &mp)
{
  queue<vector<int>> que;
  que.emplace(x);
  while (!que.empty())
  {
    vector<int> a = que.front();
    que.pop(); 
    int cou = mp[a];
    if(cou == 4) continue;
    for (int i = 0; i < a.size(); i++)
    {
      for (int j = i + 1; j < a.size(); j++)
      {
        reverse(a.begin() + i, a.begin() + j + 1);
        auto ite = mp.find(a);
        if (ite == mp.end())
        {
          mp[a] = cou + 1;
          if(cou + 1 < 4)
          que.emplace(a);
        }
        reverse(a.begin() + i, a.begin() + j + 1);
      }
    }
  }
}

int n;
int main()
{
  cin >> n;
  vector<int> a(n);
  vector<int> sorted(n);
  for (int i = 0; i < n; i++)
  {
    cin >> a[i];
    a[i]--;
  }
  for (int i = 0; i < n; i++)
  {
    sorted[i] = i;
  }

  map<vector<int>, int> mp_sorted, mp_a;
  mp_sorted[sorted] = 0;
  mp_a[a] = 0;
  rec(sorted, mp_sorted);
  rec(a, mp_a);
  int result = n - 1;
  do
  {
    auto ite = mp_sorted.find(sorted);
    auto ite2 = mp_a.find(sorted);
    if (ite != mp_sorted.end() && ite2 != mp_a.end())
    {
      result = min(result, ite->second + ite2->second);
    }
  } while (next_permutation(sorted.begin(), sorted.end()));
  cout << result << endl;
}