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 <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

struct S{
  vector<int> v;
  int t;
  S() {}
  S(vector<int>& v, int t) : v(v), t(t) {}
};

int main(){
  int N;
  while(cin>>N && N){
    vector<int> A(N);
    vector<int> goal(N);
    REP(i, N) goal[i] = i;
    REP(i, N) {
      cin>>A[i];
      A[i]--;
    }
    queue<S> que;
    que.push(S(A, 0));
    map< vector<int>, int > dist;
    while(!que.empty()){
      S s = que.front(); que.pop();
      if(s.t == 4) break;
      if(dist[s.v] != s.t) continue;
      REP(i, N)FOR(j, i + 1, N){
        vector<int> B = s.v;
        reverse(B.begin() + i, B.begin() + j + 1);
        if(dist.count(B)) continue;
        dist[B] = s.t + 1;
        que.push(S(B, s.t + 1));
      }
    }
    int ans = N - 1;
    que = queue<S>();
    que.push(S(goal, 0));
    set< vector<int> > used;
    while(!que.empty()){
      S s = que.front(); que.pop();
      if(s.t == 4) break;
      if(dist.count(s.v)){
        ans = min(ans, s.t + dist[s.v]);
      }
      REP(i, N)FOR(j, i + 1, N){
        vector<int> B = s.v;
        reverse(B.begin() + i, B.begin() + j + 1);
        if(used.count(B)) continue;
        used.insert(B);
        que.push(S(B, s.t + 1));
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}