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;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int N;
vector<int> A;
vector<int> ans;

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> N;
  A.resize(N);
  rep(i, N) cin >> A[i];
  rep(i, N) ans.push_back(i+1);

  map<vector<int>, int> rd;
  rd[ans] = 0;
  queue< vector<int> > que;
  que.push(ans);
  while(!que.empty()) {
    auto v = que.front(); que.pop();
    int tmp = rd[v];
    if(tmp >= (N-1)/2) break;
    rep(l, N-1) reps(r, l+2, N+1) {
      auto w = v;
      reverse(w.begin()+l, w.begin()+r);
      if(!rd.count(w)) {
	rd[w] = tmp + 1;
	que.push(w);
      }
    }
  }

  if(rd.count(A)) {
    cout << rd[A] << endl;
    return 0;
  }

  map<vector<int>, int> d;
  d[A] = 0;
  while(!que.empty()) que.pop();
  que.push(A);
  while(!que.empty()) {
    auto v = que.front(); que.pop();
    int tmp = d[v];
    if(tmp >= (N-1)/2) break;
    rep(l, N-1) reps(r, l+2, N+1) {
      auto w = v;
      reverse(w.begin()+l, w.begin()+r);
      if(rd.count(w)) {
	cout << rd[w] + tmp + 1 << endl;
	return 0;
      } else if(!d.count(w)) {
	d[w] = tmp + 1;
	que.push(w);
      }
    }
  }

  cout << N-1 << endl;

  return 0;
}