#include <bits/stdc++.h>
using namespace std;
int n, a[503], dp1[503][503], dp[503];
int main() {
  scanf("%d", &n);
  memset(dp1, -1, sizeof(dp1));
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    dp1[i][i] = a[i];
  }
  for (int i = n; i >= 1; i--) {
    for (int j = i + 1; j <= n; j++) {
      for (int k = i + 1; k <= j; k++) {
        if (dp1[i][k - 1] == dp1[k][j] and dp1[i][k - 1] != -1)
          dp1[i][j] = dp1[i][k - 1] + 1;
      }
    }
  }
  fill(dp + 1, dp + n + 1, 1000000009);
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = i; j >= 1; j--)
      if (dp1[j][i] > -1) dp[i] = min(dp[i], dp[j - 1] + 1);
  }
  printf("%d", dp[n]);
}
