#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int dp[505][505], p[505];
int n, a[505];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i], dp[i][i] = a[i];
  int r;
  for (int i = 2; i <= n; i++) {
    for (int l = 1; l <= n - i + 1; l++) {
      r = l + i - 1;
      for (int k = l; k < r; k++) {
        if (dp[l][k] != -1 && dp[l][k] == dp[k + 1][r]) {
          dp[l][r] = dp[l][k] + 1;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    p[i] = 2147483647;
    for (int j = 1; j <= i; j++) {
      if (dp[j][i] != -1) {
        p[i] = min(p[i], p[j - 1] + 1);
      }
    }
  }
  cout << p[n];
  return 0;
}
