#include <bits/stdc++.h>
using namespace std;
long long i, j, k, l, n, m, sum, res = 0;
long long dp[509][509], a[509], dpp[509];
long long get(long long l, long long r) {
  if (l > r) return 0;
  if (l == r) {
    dp[l][r] = a[l];
    return a[l];
  }
  if (dp[l][r] != -1) return dp[l][r];
  dp[l][r] = 0;
  for (int i = l; i < r; i++) {
    if (get(l, i) != 0 && get(i + 1, r) != 0 && get(l, i) == get(i + 1, r)) {
      dp[l][r] = get(l, i) + 1;
    }
  }
  return dp[l][r];
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  memset(dp, -1, sizeof(dp));
  get(1, n);
  for (i = 1; i <= n; i++) dpp[i] = ((long long)1e18 + 9ll);
  dpp[0] = 0;
  for (i = 1; i <= n; i++) {
    for (j = i; j > 0; j--) {
      if (dp[j][i] > 0) dpp[i] = min(dpp[i], dpp[j - 1] + 1);
    }
  }
  cout << dpp[n] << endl;
  exit(0);
}
