#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
long long int t, n, m, a[1000005] = {0}, dp[505][505] = {0}, dp2[1000] = {0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k, x, y;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  memset(dp, -1, sizeof dp);
  for (long long int l = 1; l <= n; l++) {
    for (i = 1; i <= n; i++) {
      if ((i + l - 1) > n) break;
      j = i + l - 1;
      if (i == j) {
        dp[i][j] = a[i];
        continue;
      }
      for (k = i; k < j; k++) {
        if (dp[i][k] == dp[k + 1][j] && dp[k + 1][j] != -1)
          dp[i][j] = dp[i][k] + 1;
      }
    }
  }
  for (i = 0; i < n + 5; i++) dp2[i] = i;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= i; j++) {
      if (dp[j][i] != -1) dp2[i] = min(dp2[i], dp2[j - 1] + 1);
    }
  }
  cout << dp2[n];
  return 0;
}
