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;

map<vector<int>,int> m[2];
int main() {
  int n;
  cin >> n;
  vector<int> a[2];
  for(int i=0,x; i<n; i++) {
    cin >> x;
    a[0].push_back(x);
    a[1].push_back(i+1);
  }
  for(int k=0; k<2; k++) {
    queue<vector<int> > que;
    m[k][a[k]]=0;
    que.push(a[k]);
    while(!que.empty()) {
      vector<int> b=que.front();que.pop();
      int x=m[k][b];
      if(x>3) continue;
      for(int i=0; i<n; i++) {
        for(int j=i+1; j<n; j++) {
          vector<int> c=b;
          reverse(c.begin()+i,c.begin()+j+1);
          if(m[k].count(c)) continue;
          m[k][c]=x+1;
          que.push(c);
        }
      }
    }
  }
  int ans=9;
  for(map<vector<int>,int>::iterator it=m[0].begin(); it!=m[0].end(); it++) {
    if(m[1].count(it->first)) ans=min(ans,it->second+m[1][it->first]);
  }
  cout << ans << endl;
  return 0;
}