#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 10;
int n, a[MAXN], f[MAXN][MAXN], g[MAXN], ans;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), f[i][i] = a[i];
  for (int len = 2; len <= n; ++len) {
    for (int l = 1; l <= n - len + 1; ++l) {
      int r = l + len - 1;
      for (int mid = l; mid < r; ++mid) {
        if ((f[l][mid] == f[mid + 1][r]) && f[l][mid]) {
          f[l][r] = f[l][mid] + 1;
          break;
        }
      }
    }
  }
  memset(g, 0x3c, sizeof g);
  g[0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < i; ++j) {
      if (f[j + 1][i]) {
        g[i] = min(g[i], g[j] + 1);
      }
    }
  }
  printf("%d\n", g[n]);
  return 0;
}
