#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long double PI = 2 * acos(0.0);
const vector<long long int> dx = {1, -1, 0, 0};
const vector<long long int> dy = {0, 0, 1, -1};
template <typename T = long long int>
vector<T> ga(long long int n, bool oneIndexed = false) {
  vector<T> a = vector<T>(n + oneIndexed);
  for (long long int i = 0; i < n; i++) {
    T p;
    cin >> p;
    a[i + oneIndexed] = p;
  }
  return move(a);
}
template <typename T, typename A>
void pa(vector<T, A> const &a, long long int begin = 0,
        long long int end = -1) {
  if (end == -1) end = (long long int)a.size() - 1;
  for (long long int i = begin; i <= end; i++) {
    cout << a[i] << " ";
  }
  cout << "\n";
}
template <typename T, typename A>
void papair(vector<T, A> const &a, long long int begin = 0,
            long long int end = -1) {
  if (end == -1) end = (long long int)a.size() - 1;
  for (long long int i = begin; i <= end; i++) {
    cout << a[i].first << " " << a[i].second << "\n";
  }
}
pair<long long int, long long int> dp[600][600];
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> arr = ga(n);
  if (n == 1) {
    cout << 1;
    return;
  }
  for (long long int len = 2; len <= n; len++) {
    for (long long int i = 0; i < n - len + 1; i++) {
      if (len == 2) {
        if (arr[i] == arr[i + 1])
          dp[len][i] = {1, arr[i] + 1};
        else {
          dp[len][i] = {2, -1};
        }
        continue;
      }
      dp[len][i] = {len, -1};
      if (len == 3) {
        if (dp[len - 1][i].second != -1) {
          if (dp[len - 1][i].second == arr[i + 2]) {
            dp[len][i] = {1, arr[i + 2] + 1};
          } else {
            dp[len][i] = {2, -1};
          }
        }
        if (dp[len - 1][i + 1].second != -1) {
          if (dp[len - 1][i + 1].second == arr[i]) {
            dp[len][i] = {1, arr[i] + 1};
          } else {
            dp[len][i] = {2, -1};
          }
        }
        continue;
      }
      for (long long int j = i + 2; j < i + len - 1; j++) {
        if (dp[j - i][i].second != -1 &&
            dp[len - j + i][j].second == dp[j - i][i].second)
          dp[len][i] = {1, dp[j - i][i].second + 1};
        else {
          if (dp[len][i].first > dp[len - j + i][j].first + dp[j - i][i].first)
            dp[len][i] = {dp[len - j + i][j].first + dp[j - i][i].first, -1};
        }
      }
      if (dp[len - 1][i].first == 1) {
        if (dp[len - 1][i].second == arr[i + len - 1]) {
          dp[len][i] = {1, arr[i + len - 1] + 1};
        } else {
          dp[len][i] = {2, -1};
        }
      }
      if (dp[len - 1][i + 1].first == 1) {
        if (dp[len - 1][i + 1].second == arr[i]) {
          dp[len][i] = {1, arr[i] + 1};
        } else {
          dp[len][i] = {2, -1};
        }
      }
    }
  }
  cout << dp[n][0].first;
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
