/*
multiset <int> s;

int main()
{
  s.insert(3);
  s.insert(3);
  s.erase(s.find(3));

  multiset <int>::iterator it = s.find(3);

  if (s.find(3) == s.end()) 
    ;
  if (s.count(3) == 0)
    ;

  for (multiset <int>::iterator it = s.begin(); it != s.end(); it++)
    ;
}
*/

// n, a[0..n-1]
// reverse(l, r)

#include <cstdio>
#include <cassert>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)

typedef vector <int> vi;
typedef unsigned long long ll;

queue <pair<vi, int> > q;

const int N = (int)2e6 + 3;

ll s[N];

bool insert( ll h )
{
  int i = h % N;
  while (s[i] && s[i] != h)
    if (++i == N)
      i = 0;
  if (s[i] == h)
    return 0;
  s[i] = h;
  return 1;
}

ll Hash( vi &v )
{
  ll h = 0;
  forn(i, v.size())
    h = h * 239 + v[i];
  return h;
}

inline void add( vi &v, int d )
{
  if (insert(Hash(v))) 
    q.push(make_pair(v, d));
}

bool sorted( vi &v )
{
  forn(i, v.size())
    if (v[i] != i + 1)
      return 0;
  return 1;
}

vi t, v;
int d = -1;

void make_t( vi &v, int l, int r )
{
  forn(i, v.size())
    t[i] = v[i];
  reverse(t.begin() + l, t.begin() + r);
}

bool queue_do()
{
  assert(q.size());
  d = q.front().second;
  v = q.front().first;
  q.pop();
  return sorted(v);
}

int main()
{
  int n;
  scanf("%d", &n);

  v = vi(n);
  t = vi(n);
  forn(i, n)
    scanf("%d", &v[i]);

  add(v, 0);
  while (1)
  {
    //puts("!");
    if (queue_do())
      break;
    forn(r, n + 1) // r <= n
      forn(l, r) // l < r, [l..r)
      {
        make_t(v, l, r);
        add(t, d + 1);
      }
  }
  printf("distance = %d\n", d);
  return 0;
}