#include <bits/stdc++.h>
using namespace std;
const int N = 520;
int n, a[N][N], f[N];
int cost[500], dp[500][500];
int main() {
  scanf("%d", &n);
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++) scanf("%d", &dp[i][i]);
  for (int len = 2; len <= n; len++) {
    for (int left = 0; left + len - 1 < n; left++) {
      int right = left + len - 1;
      for (int t = left; t < right; t++) {
        if (dp[left][t] == dp[t + 1][right] && dp[left][t] != -1)
          dp[left][right] = dp[left][t] + 1;
      }
    }
  }
  cost[0] = 0;
  for (int i = 1; i <= n; i++) {
    cost[i] = 0x3f3f3f3f;
    for (int j = 0; j < i; j++) {
      if (dp[j][i - 1] != -1) {
        cost[i] = min(cost[i], cost[j] + 1);
      }
    }
  }
  printf("%d\n", cost[n]);
}
