#include <bits/stdc++.h>
using namespace std;
long long bin_pow(long long a, long long b) {
  if (b == 0) {
    return 1;
  } else if (b % 2 == 0) {
    return bin_pow(a, b / 2) * bin_pow(a, b / 2);
  } else {
    b -= 1;
    return bin_pow(a, b / 2) * bin_pow(a, b / 2) * a;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  vector<long long> a(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<vector<pair<long long, pair<long long, long long>>>> dp(
      n, vector<pair<long long, pair<long long, long long>>>(
             n, {INT_MAX, {-1, -1}}));
  for (long long i = 0; i < n; i++) {
    dp[i][i] = {1, {a[i], a[i]}};
  }
  for (long long i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1]) {
      dp[i][i + 1] = {1, {a[i] + 1, a[i] + 1}};
    } else {
      dp[i][i + 1] = {2, {a[i], a[i + 1]}};
    }
  }
  for (long long d = 2; d < n; d++) {
    for (long long i = 0; i < n; i++) {
      long long j = i + d;
      if (j >= n) {
        break;
      }
      for (long long k = i; k < j; k++) {
        long long p = dp[i][k].first + dp[k + 1][j].first;
        if (dp[i][k].second.second == dp[k + 1][j].second.first) {
          p -= 1;
        }
        if (p < dp[i][j].first) {
          dp[i][j].first = p;
          if (p == 1) {
            dp[i][j].second = {dp[i][k].second.second + 1,
                               dp[i][k].second.second + 1};
            continue;
          }
          if (dp[i][k].second.second == dp[k + 1][j].second.first &&
              dp[k + 1][j].first == 1) {
            dp[i][j].second = dp[i][k].second;
            dp[i][j].second.second += 1;
          } else if (dp[i][k].second.second == dp[k + 1][j].second.first &&
                     dp[i][k].first == 1) {
            dp[i][j].second = dp[k + 1][j].second;
            dp[i][j].second.first += 1;
          } else {
            dp[i][j].second.first = dp[i][k].second.first;
            dp[i][j].second.second = dp[k + 1][j].second.second;
          }
          if (dp[i][j].first == 2 &&
              dp[i][j].second.second == dp[i][j].second.first) {
            dp[i][j].first = 1;
            dp[i][j].second = {dp[i][j].second.first + 1,
                               dp[i][j].second.first + 1};
          } else if (dp[i][j].first == 2 &&
                     dp[i][j].second.second == dp[i][j].second.first) {
            dp[i][j].first = 1;
            dp[i][j].second = {dp[i][j].second.second + 1,
                               dp[i][j].second.second + 1};
          }
        }
      }
    }
  }
  cout << dp[0][n - 1].first;
  return 0;
}
