#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
constexpr int N = 5e2 + 5;
constexpr int mod = 998244353;
int n, arr[N], dp[N][N], ans = 0, t[N][N];
void Solve() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  fill(&t[0][0], &t[N - 1][N], n);
  for (int i = 0; i < n; i++) {
    dp[i][i] = arr[i];
    t[i][i] = 1;
    if (i + 1 < n) {
      dp[i][i + 1] = (arr[i] == arr[i + 1] ? arr[i] + 1 : 0);
      if (arr[i] == arr[i + 1])
        ans = 1, t[i][i + 1] = 1;
      else
        t[i][i + 1] = 2;
    }
  }
  for (int len = 2; len < n; len++) {
    for (int l = 0, r; l + len < n; l++) {
      r = l + len;
      for (int k = l; k < r; k++)
        if (dp[l][k] == dp[k + 1][r] && dp[k + 1][r] != 0) {
          dp[l][r] = dp[l][k] + 1, ans = max(ans, len);
          t[l][r] = 1;
        } else
          t[l][r] = min(t[l][k] + t[k + 1][r], t[l][r]);
    }
  }
  cout << t[0][n - 1];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}
