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 <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N;

map<vector<int>, int> bfs(vector<int> s, int depth) {
  map<vector<int>, int> dp;
  queue<vector<int> > q;
  q.push(s);
  dp[s] = 0;

  while (!q.empty()) {
    vector<int> x = q.front(); q.pop();
    int f = dp[x];
    if (f+1 >= depth) continue;

    int n = x.size();
    for (int l=0; l<n; l++) {
      for (int r=l+1; r<n; r++) {
        vector<int> t(x);
        rep(s, (r-l+1)/2) swap(t[l+s], t[r-s]);
        if (dp.find(t) == dp.end()) {
          dp[t] = f+1;
          q.push(t);
        }
      }
    }
  }
  return dp;
}

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N;
  vector<int> s;
  rep(i, N) {
    int a;
    cin >> a;
    a--;
    s.pb(a);
  }

  vector<int> id(N);
  rep(i, N) id[i] = i;

  int m = N-1;
  auto lp = bfs(id, 4), rp = bfs(s, 4);
  for (auto &p : lp) {
    if (rp.find(p._1) == rp.end()) continue;
    m = min(m, p._2 + rp[p._1]);
  }
  cout << m << "\n";
  return 0;
}