#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;
  }
}