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 <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int a[10];

int encode(int x[10], int n) {
  int res = 0;
  REP(i,n-1) {
    res = 10 * res + x[i];
  }
  return res;
}

void decode(int x[10], int n, int code) {
  int bit = 0;
  for (int i=n-2; i>=0; --i) {
    x[i] = code%10;
    bit |= 1<<x[i];
    code /= 10;
  }
  REP(i,n) if (!(bit>>i&1)) x[n-1] = i;
}

int now[10];

int main() {
  int n;
  cin >> n;
  REP(i,n) {
    cin >> a[i];
    --a[i];
  }
  queue<int> Q;
  int start = encode(a,n);
  Q.push(start);
  map<int, int> mp;
  mp[start] = 0;
  while(!Q.empty()) {
    int p = Q.front(); Q.pop();
    decode(now, n, p);
    int d = mp[p];
    REP(i,n) {
      REP(j,i) {
        reverse(now+j, now+i+1);
        int next = encode(now,n);
        if (mp.count(next) == 0) {
          mp[next] = d+1;
          if (d<4) {
            Q.push(next);
          }
        }
        reverse(now+j, now+i+1);        
      }
    }
  }
  REP(i,n) now[i]=i;
  start = encode(now,n);
  Q.push(start);
  map<int,int> mp2;
  mp2[start] = 0;
  int ans = INF;
  
  while(!Q.empty()) {
    int p = Q.front(); Q.pop();
    decode(now, n, p);
    int d = mp2[p];

    if (mp.count(p)) {
      ans = min(ans, mp[p] + mp2[p]);
    }
    if (d == 4) continue;
    REP(i,n) {
      REP(j,i) {
        reverse(now+j, now+i+1);
        int next = encode(now,n);
        if (mp2.count(next) == 0) {
          mp2[next] = d+1;
          Q.push(next);
        }
        reverse(now+j, now+i+1);        
      }
    }
  }
  if (ans == INF) {
    cout << 9 << endl;
  } else {
    cout << ans << endl;
  }
}