#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline long long read() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}
void put1() { puts("YES"); }
void put2() { puts("NO"); }
void put3() { puts("-1"); }
long long qpf(long long a, long long b, long long p) {
  long long ret = 0;
  while (b) {
    if (b & 1) ret = (ret + a) % p;
    a = (a + a) % p;
    b >>= 1;
  }
  return ret % p;
}
long long qp(long long a, long long n, long long p) {
  long long ret = 1;
  while (n) {
    if (n & 1) ret = qpf(ret, a, p);
    a = qpf(a, a, p);
    n >>= 1;
  }
  return ret % p;
}
const int manx = 1e3 + 5;
long long dp[manx][manx], a[manx][manx];
int main() {
  long long n = read();
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) dp[i][j] = j - i + 1;
  for (int i = 1; i <= n; i++) a[i][i] = read(), dp[i][i] = 1;
  for (int len = 2; len <= n; len++)
    for (int l = 1; l + len - 1 <= n; l++) {
      int r = l + len - 1;
      for (int k = l; k < r; k++) {
        dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);
        if (dp[l][k] == 1 && dp[k + 1][r] == 1 && a[l][k] == a[k + 1][r])
          dp[l][r] = 1, a[l][r] = a[l][k] + 1;
      }
    }
  printf("%lld", dp[1][n]);
  return 0;
}
