#include <bits/stdc++.h>
using namespace std;
long double eps = (long double)1 / 1e15;
long long inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
long long sqr(long long a) { return a * a; }
long long qb(long long a) { return a * a * a; }
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
long long binpow(long long a, long long b, long long mod) {
  return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod
                    : sqr(binpow(a, b / 2, mod)) % mod)
           : 1;
}
long long binmult(long long a, long long b, long long mod) {
  return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod
                    : (2 * binmult(a, b / 2, mod)) % mod)
           : 0;
}
vector<int> st;
map<vector<int>, int> ump;
int go(vector<int> vc) {
  if (vc.empty()) return 0;
  if (ump.find(vc) != ump.end()) return ump[vc];
  int mn = 1e9, i, j, k, ans = 1e9;
  for (auto f : vc) mn = min(mn, f);
  for (i = 0; i < vc.size(); i++) {
    if (vc[i] == mn) {
      for (j = i + 1; j < vc.size(); j++)
        if (vc[j] != vc[i]) break;
      break;
    }
  }
  if ((j - i) % 2) {
    vector<int> n1, n2;
    for (k = 0; k < i; k++) n1.push_back(vc[k]);
    for (k = i; k < vc.size(); k++) n2.push_back(vc[k]);
    for (k = i; k < j; k++) {
      n2.erase(n2.begin());
      ans = min(ans, go(n1) + go(n2) + 1);
      n1.push_back(vc[k]);
    }
  } else {
    for (k = i; k < j; k += 2) vc[k]++;
    for (k = i + 1; k < j; k++, j--) vc.erase(vc.begin() + k);
    ans = go(vc);
  }
  ump.insert({vc, ans});
  return ans;
}
int main() {
  cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0);
  int n, i, v;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> v, st.push_back(v);
  cout << go(st);
  return 0;
}
