#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
ll pwr(ll a, ll b);
const int N = 505;
int dpL[N][N], dp[N][N];
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> dp[i][i];
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i; j <= n; ++j) {
      dpL[i][j] = j - i + 1;
    }
  }
  for (int l = 2; l <= n; l++) {
    for (int i = 1; i <= n - l + 1; i++) {
      int j = i + l - 1;
      for (int k = i; k < j; k++) {
        dpL[i][j] = min(dpL[i][j], dpL[i][k] + dpL[k + 1][j]);
        if (dpL[i][k] == 1 and dpL[k + 1][j] == 1 and
            dp[i][k] == dp[k + 1][j]) {
          dpL[i][j] = 1;
          dp[i][j] = dp[i][k] + 1;
        }
      }
    }
  }
  cout << dpL[1][n] << "\n";
}
ll pwr(ll a, ll b) {
  a %= mod;
  ll res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
