#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a;
int dp[501][501];
int solve(int l, int r) {
  if (l == r) {
    return a[l];
  }
  if (dp[l][r] != 0) {
    return dp[l][r];
  }
  dp[l][r] = -1;
  for (int i = l; i < r; i++) {
    int x = solve(l, i), y = solve(i + 1, r);
    if (x != -1 && y != -1 && x == y) {
      dp[l][r] = x + 1;
      break;
    }
  }
  return dp[l][r];
}
int dp2[501];
int sol(int x) {
  if (x == n) {
    return 0;
  }
  if (~dp2[x]) {
    return dp2[x];
  }
  int ans = 1e9;
  for (int i = x; i < n; i++) {
    if (solve(x, i) != -1) {
      ans = min(ans, 1 + sol(i + 1));
    }
  }
  return dp2[x] = ans;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  memset(dp, 0, sizeof dp);
  memset(dp2, -1, sizeof dp2);
  cout << sol(0) << endl;
}
