#include <bits/stdc++.h>
using namespace std;
const int MAXN = 505;
int N;
int a[MAXN];
int dp[MAXN][MAXN];
int f[MAXN][MAXN];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) {
    scanf("%d", &a[i]);
    dp[i][i] = 1;
    f[i][i] = a[i];
  }
  for (int i = 1; i <= N; ++i) {
    for (int j = i + 1; j <= N; ++j) {
      dp[i][j] = j - i + 1;
    }
  }
  int len;
  for (int i = 2; i <= N; ++i) {
    for (int j = 1; j <= N - i + 1; ++j) {
      len = i + j - 1;
      for (int k = j; k <= len - 1; ++k) {
        if (f[j][k] && f[k + 1][len] && f[j][k] == f[k + 1][len]) {
          f[j][len] = f[j][k] + 1;
          dp[j][len] = 1;
        }
      }
    }
  }
  for (int i = 2; i <= N; ++i) {
    for (int j = 1; j <= N - i + 1; ++j) {
      len = i + j - 1;
      for (int k = j; k <= len - 1; ++k) {
        dp[j][len] = min(dp[j][len], dp[j][k] + dp[k + 1][len]);
      }
    }
  }
  printf("%d", dp[1][N]);
  return 0;
}
