#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(time(NULL));
int n, v[2001], a[2001], m, k, b[2001], dp[501][501], dp2[501];
vector<int> l[2002];
bool use[2001];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> v[i];
  for (int i = 1; i <= n; i++) {
    dp[i][i] = v[i];
    l[i].push_back(i);
  }
  for (int lg = 2; lg <= n; lg++)
    for (int i = 1; i <= n; i++) {
      int j = i + lg - 1;
      if (j > n) break;
      for (int p = i; p < j; p++)
        if (dp[i][p] == dp[p + 1][j] && dp[i][p] != 0) {
          dp[i][j] = dp[i][p] + 1;
          break;
        }
      if (dp[i][j]) l[j].push_back(i);
    }
  for (int i = 1; i <= n; i++) {
    dp2[i] = 1e9;
    for (auto j : l[i]) dp2[i] = min(dp2[i], 1 + dp2[j - 1]);
  }
  cout << dp2[n];
  return 0;
}
