#include <bits/stdc++.h>
using namespace std;
int a[505], d[505][505], len[505];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    d[i][i] = a[i];
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j + i <= n + 1; j++) {
      int t = j + i - 1;
      for (int k = j; k < t; k++) {
        if (d[j][k] && d[j][k] == d[k + 1][t]) d[j][t] = d[j][k] + 1;
      }
    }
  }
  memset(len, 0x3f, sizeof(len));
  len[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < i; j++) {
      if (d[j + 1][i]) len[i] = min(len[i], len[j] + 1);
    }
  }
  cout << len[n] << endl;
}
