#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vector<T>>;
const ll MOD = 1e9 + 7;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}
int main() {
  init();
  ll N;
  cin >> N;
  vl A(N);
  for (ll i = 0; i < (N); i++) cin >> A[i];
  vvc<ll> dp(N + 1, vl(N, INF)), V(N + 1, vl(N, INF));
  for (ll i = 0; i < (N); i++) V[1][i] = A[i], dp[1][i] = 1;
  for (ll w = (2); w < (N + 1); w++) {
    for (ll i = 0; i < (N - w + 1); i++) {
      for (ll j = (1); j < (w); j++) {
        ll l = dp[j][i], r = dp[w - j][i + j];
        if (l == 1 && r == 1 && V[j][i] == V[w - j][i + j])
          dp[w][i] = 1, V[w][i] = V[j][i] + 1;
        else
          chmin(dp[w][i], dp[j][i] + dp[w - j][i + j]);
      }
    }
  }
  cout << dp[N][0] << endl;
}
