#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
void solve() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int& x : a) scanf("%d", &x);
  vector<vector<int> > dp(n, vector<int>(n));
  for (int len = 1; len <= n; len++) {
    for (int i = 0; i <= n - len; i++) {
      int j = i + len - 1;
      if (len == 1) {
        dp[i][j] = a[i];
      } else {
        dp[i][j] = -1;
        for (int k = i; k < j; k++) {
          if (dp[i][k] != -1 && dp[k + 1][j] != -1 &&
              dp[i][k] == dp[k + 1][j]) {
            dp[i][j] = dp[i][k] + 1;
          }
        }
      }
    }
  }
  vector<int> dp2(n + 1);
  for (int i = 1; i <= n; i++) {
    dp2[i] = n;
    for (int j = 0; j < i; j++) {
      if (dp[j][i - 1] != -1) {
        dp2[i] = min(dp2[i], dp2[j] + 1);
      }
    }
  }
  printf("%d\n", dp2[n]);
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
