#include <bits/stdc++.h>
using namespace std;
const int maxn = 500 + 5;
int T, n, m;
int A[maxn], dp[maxn][maxn], f[maxn];
int book[maxn][maxn];
int calc(int l, int r) {
  if (book[l][r]) return dp[l][r];
  if (l == r) return dp[l][r] = A[l];
  for (int k = l; k < r; ++k) {
    int a = calc(l, k), b = calc(k + 1, r);
    if (a != -1 && b != -1 && a == b) {
      dp[l][r] = a + 1;
    }
  }
  book[l][r] = 1;
  return dp[l][r];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
  }
  memset(dp, -1, sizeof(dp));
  memset(f, 0x3f, sizeof(f));
  f[0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= i; ++j) {
      if (calc(j, i) != -1) f[i] = min(f[j - 1] + 1, f[i]);
    }
  }
  cout << f[n] << endl;
  return 0;
}
