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 <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstdlib>

using namespace std;

typedef long long lli;
typedef pair<lli, int> P;
const int L = 4;
lli n, in;
vector<lli> input;
map<lli, int> data;
set<lli> vis;

lli rev(lli num, int a, int b){
  string str;
  while(num > 0){
    str += (char)((int)(num % 10LL) + '0');
    num /= 10LL;
  }
  reverse(str.begin(), str.end());
  if(str.size() != n) str = "0" + str;
  reverse(str.begin() + a, str.begin() + b + 1);
  return strtoll(str.c_str(), NULL, 10);

}

int solve(){
  lli state = 0;
  for(int i=0;i<n;i++){
    state *= 10;
    if(input[i] != 10) state += input[i];
  }
  if(data.find(state) != data.end()) return data[state];
  vis.clear();
  P u = P(state, 0), v;
  queue<P> q;
  q.push(u);
  while(!q.empty()){
    u = q.front();
    q.pop();
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        if(i == j) continue;
        v = P(rev(u.first, i, j), u.second + 1);
        if(vis.find(v.first) == vis.end()){
          vis.insert(v.first);
          if(data.find(v.first) != data.end()) return data[v.first] + v.second;
          if(v.second < L) q.push(v);
        }
      }
    }
  }
  return n-1;
}

void init(){
  data.clear();
  lli state = 0;
  for(int i=1;i<=n;i++){
    state *= 10;
    if(i != 10) state += i;
  }
  P u = P(state, 0), v;
  queue<P> q;
  q.push(u);
  data[state] = 0;
  while(!q.empty()){
    u = q.front();
    q.pop();
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        if(i == j) continue;
        v = P(rev(u.first, i, j), u.second + 1);
        if(data.find(v.first) == data.end()){
          data[v.first] = v.second;
          if(v.second < L) q.push(v);
        }
      }
    }
  }
}

int main(){
  while(cin >> n){
    input.clear();
    for(int i=0;i<n;i++){
      cin >> in;
      input.push_back(in);
    }
    init();
    cout << solve() << endl;
  }
}