#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0x3f3f3f3f));
  vector<vector<int>> c(n + 1, vector<int>(n + 1, 0x3f3f3f3f));
  for (int i = 1; i <= n; i++) cin >> a[i], c[i][i] = a[i], dp[i][i] = 1;
  for (int len = 2; len <= n; len++) {
    for (int l = 1, r = l + len - 1; r <= n; r++, l++) {
      dp[l][r] = min(dp[l + 1][r], dp[l][r - 1]) + 1;
      for (int k = l; k < r; k++) {
        dp[l][r] = min(dp[l][k] + dp[k + 1][r], dp[l][r]);
        if (dp[l][k] == 1 && dp[k + 1][r] == 1 && c[l][k] == c[k + 1][r]) {
          dp[l][r] = min(dp[l][r], 1);
          c[l][r] = c[l][k] + 1;
        }
      }
    }
  }
  cout << dp[1][n] << '\n';
}
