#include <bits/stdc++.h>
using namespace std;
const int N = 500;
int aa[N], dp[N][N], dq[N];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> aa[i];
  for (int i = n - 1; i >= 0; i--) {
    dp[i][i] = aa[i];
    for (int j = i + 1; j < n; j++)
      for (int k = i; k < j; k++)
        if (dp[i][k] && dp[k + 1][j] && dp[i][k] == dp[k + 1][j]) {
          dp[i][j] = dp[i][k] + 1;
          break;
        }
  }
  for (int j = 0; j < n; j++) {
    int x = n;
    for (int i = 0; i <= j; i++)
      if (dp[i][j]) x = min(x, (i > 0 ? dq[i - 1] : 0) + 1);
    dq[j] = x;
  }
  cout << dq[n - 1] << '\n';
  return 0;
}
