#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long long N = 501;
const long long INF = 1e9 + 7;
long long a[N];
long long dp[N][N];
long long n;
long long cal(long long l, long long r) {
  if (l < 0 || r >= n) return 0;
  long long &ans = dp[l][r];
  if (ans != -1) return ans;
  ans = 0;
  for (long long k = l + 1; k <= r; k++) {
    long long lft = cal(l, k - 1);
    long long rit = cal(k, r);
    if (lft == rit && lft > 0) {
      ans = lft + 1;
      return ans;
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    dp[i][i] = a[i];
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = i + 1; j < n; j++) {
      cal(i, j);
    }
  }
  long long len[N];
  memset(len, INF, sizeof(len));
  len[0] = 0;
  len[1] = 1;
  for (long long r = 1; r <= n; r++) {
    for (long long l = 0; l < r; l++) {
      if (cal(l, r - 1) > 0) {
        len[r] = min(len[r], len[l] + 1LL);
      }
    }
  }
  cout << len[n] << '\n';
  return 0;
}
