#include <bits/stdc++.h>
using namespace std;
int dp[501][501], n, dis[1001], a[501];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i) dp[i][i] = a[i];
  for (int i = 2; i <= n; ++i) {
    for (int l = 1; l + i - 1 <= n; ++l) {
      int r = l + i - 1;
      for (int k = l; k < r; ++k)
        if (dp[l][k] && dp[k + 1][r] && dp[l][k] == dp[k + 1][r])
          dp[l][r] = max(dp[l][r], dp[l][k] + 1);
    }
  }
  dis[n + 1] = 0;
  for (int i = n; i >= 1; --i) {
    dis[i] = 0x3f3f3f3f;
    for (int j = i; j <= n; ++j) {
      if (dp[i][j]) dis[i] = min(dis[i], dis[j + 1] + 1);
    }
  }
  printf("%d\n", dis[1]);
  return 0;
}
