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;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const ld eps = 1e-9;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
struct Elm {
  int d;
  vi v;
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n; cin >> n;
  vi a(n);
  for(int i=0;i<n;++i) {
    cin >> a[i];
    a[i]--;
  }
  queue<Elm> pq;
  map<vi, P> mp;
  map<vi, bool> upd1, upd2;
  pq.push({0, a});
  mp[a].first = 0;
  upd1[a] = true;
  while(!pq.empty()) {
    auto now = pq.front(); pq.pop();
    if(upd1[now.v] && mp[now.v].first < now.d) continue;
    for(int i=0;i<n;++i) {
      for(int j=i+1;j<n;++j) {
        for(int k=0;k<(j-i+1)/2;++k) {
          swap(now.v[i+k], now.v[j-k]);
        }
        if((!upd1[now.v] || mp[now.v].first > now.d + 1) && now.d < 4) {
          upd1[now.v] = true;
          mp[now.v].first = now.d + 1;
          if(now.d+1 < 4) pq.push({now.d + 1, now.v});
        }
        for(int k=0;k<(j-i+1)/2;++k) {
          swap(now.v[i+k], now.v[j-k]);
        }
      }
    }
  }
  vi init(n);
  for(int i=0;i<n;++i) {
    init[i] = i;
  }
  pq.push({0, init});
  mp[init].second = 0;
  upd2[init] = true;
  while(!pq.empty()) {
    auto now = pq.front(); pq.pop();
    if(upd2[now.v] && mp[now.v].second < now.d) continue;
    for(int i=0;i<n;++i) {
      for(int j=i+1;j<n;++j) {
        for(int k=0;k<(j-i+1)/2;++k) {
          swap(now.v[i+k], now.v[j-k]);
        }
        if((!upd2[now.v] || mp[now.v].second > now.d + 1) && now.d < 4) {
          upd2[now.v] = true;
          mp[now.v].second = now.d + 1;
          if(now.d+1 < 4) pq.push({now.d + 1, now.v});
        }
        for(int k=0;k<(j-i+1)/2;++k) {
          swap(now.v[i+k], now.v[j-k]);
        }
      }
    }
  }
  int ans = n-1;
  for(auto &e: mp) {
    if(upd1[e.first] && upd2[e.first]) ans = min(ans, e.second.first + e.second.second);
  }
  cout << ans << endl;
}