#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false),
            cout << fixed << setprecision(9), 0);
int dp[510][510];
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &a : A) cin >> a;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j <= N; j++) {
      dp[i][j] = 1e9;
    }
  }
  for (int i = 0; i < N; i++) {
    dp[i][i + 1] = -A[i];
  }
  for (int n = 2; n <= N; n++) {
    for (int i = 0; i + n <= N; i++) {
      int k = i + n;
      for (int j = i + 1; j < k; j++) {
        int a = (dp[i][j] < 0 ? 1 : dp[i][j]);
        int b = (dp[j][k] < 0 ? 1 : dp[j][k]);
        if (a == 1 && b == 1 && dp[i][j] == dp[j][k]) {
          dp[i][k] = dp[i][j] - 1;
        } else {
          dp[i][k] = min(dp[i][k], a + b);
        }
      }
    }
  }
  cout << (dp[0][N] < 0 ? 1 : dp[0][N]) << endl;
  return 0;
}
