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;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, vector<int> > P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int n;
vector<int> a;

ll f(const vector<int>& v){
  ll res=0;
  rep(i,n){
    res*=10LL;
    res+=v[i];
  }
  return res;
}

map<ll,int> d1,d2;

int main(){
  cin>>n;
  rep(i,n){
    int b;
    cin>>b;
    a.push_back(b);
  }
  d1[f(a)]=0;
  queue<vector<int> > que;
  que.push(a);
  while(que.size()){
    vector<int> crt=que.front();
    que.pop();
    if(d1[f(crt)]>=(n/2)-1)continue;
    rep(i,n)repl(j,i+1,n){
      vector<int> nxt=crt;
      reverse(nxt.begin()+i,nxt.begin()+j+1);
      if(d1.find(f(nxt))!=d1.end())continue;
      d1[f(nxt)]=d1[f(crt)]+1;
      que.push(nxt);
    }
  }

  vector<int> p;
  rep(i,n)p.push_back(i+1);
  que.push(p);
  d2[f(p)]=0;
  while(que.size()){
    vector<int> crt=que.front();
    que.pop();
    if(d1.find(f(crt))!=d1.end()){
      cout<<d1[f(crt)]+d2[f(crt)]<<endl;
      return 0;
    }
    rep(i,n)repl(j,i+1,n){
      vector<int> nxt=crt;
      reverse(nxt.begin()+i,nxt.begin()+j+1);
      if(d2.find(f(nxt))!=d2.end())continue;
      d2[f(nxt)]=d2[f(crt)]+1;
      que.push(nxt);
    }
  }
  return 0;
}