#include <bits/stdc++.h>
using namespace std;
using cd = complex<double>;
const long long mod = 1e9 + 7;
long long INF = 1e15;
void Fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int n;
int a[505];
int dp[505][505];
int dp2[505];
int FindDP(int l, int r) {
  if (l == r) {
    return dp[l][r] = a[l];
  }
  if (dp[l][r] > 0 or dp[l][r] == -2) {
    return dp[l][r];
  }
  for (int mid = l; mid < r; mid++) {
    int ansl = FindDP(l, mid), ansr = FindDP(mid + 1, r);
    if (ansl != -2 and ansl == ansr) {
      return dp[l][r] = ansl + 1;
    }
  }
  return dp[l][r] = -2;
}
signed main() {
  Fastio();
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    for (int j = 1; j <= n; j++) {
      dp[i][j] = -1;
    }
  }
  for (int i = 1; i <= n; i++) {
    dp2[i] = 1e9;
    for (int j = i - 1; j >= 0; j--) {
      if (FindDP(j + 1, i) > 0) {
        dp2[i] = min(dp2[i], dp2[j] + 1);
      }
    }
  }
  cout << dp2[n];
}
