#include <bits/stdc++.h>
using namespace std;
int dp[505][505];
int a[505][505];
int main() {
  int n;
  int i, j, k;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i][i];
    for (j = i; j <= n; j++) dp[i][j] = j - i + 1;
  }
  for (int len = 2; len <= n; len++) {
    for (i = 1; i + len - 1 <= n; i++) {
      j = i + len - 1;
      for (k = i; k < j; k++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
        if (dp[i][k] == 1 && dp[k + 1][j] == 1 && a[i][k] == a[k + 1][j]) {
          dp[i][j] = 1;
          a[i][j] = a[i][k] + 1;
        }
      }
    }
  }
  cout << dp[1][n] << endl;
  return 0;
}
