#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC target("avx2")
using namespace std;
void Debug(char *s) {
  if (!0) return;
  cout << s << endl;
}
void Debug(int val) {
  if (!0) return;
  cout << val << endl;
}
void Debug(double val) {
  if (!0) return;
  cout << val << endl;
}
void Debug(string s) {
  if (!0) return;
  cout << s << endl;
}
void Debug(long long val) {
  if (!0) return;
  cout << val << endl;
}
void Debug() {
  if (!0) return;
  cout << endl;
}
template <class T, class... Args>
void Debug(T value, Args... args) {
  if (!0) return;
  cout << value << " ";
  Debug(args...);
}
void randrng() {
  mt19937 rng(1007050321);
  cout << rng() << '\n';
}
long long mod = 1e9 + 7;
long long quicl_mul(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return ans;
}
long long Com(long long m, long long n) {
  if (m > n) return 0;
  long long a, b;
  a = b = 1;
  for (int i = m + 1; i <= n; i++) a = a * i % mod;
  for (int i = 1; i <= n - m; i++) b = (b * i) % mod;
  long long ans = a % mod * (quicl_mul(b, mod - 2) % mod) % mod;
  return ans;
}
void redirect(int status) {
  if (!status) return;
  freopen("C:\\Users\\18378\\Documents\\cb-code\\test12\\in.txt", "r", stdin);
}
int a[507];
int dp[507];
void c_plus_plus_main() {
  ios::sync_with_stdio(false);
  redirect(0);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  dp[n + 1] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = n; i >= 1; i--) {
    stack<int> stk;
    stk.push(a[i]);
    dp[i] = dp[i + 1] + 1;
    for (int j = i + 1; j <= n; j++) {
      if (stk.top() == a[j]) {
        int x = a[j];
        while (1) {
          if (stk.size() == 0) {
            stk.push(x);
            break;
          } else {
            if (stk.top() == x) {
              stk.pop();
              x = x + 1;
            } else {
              stk.push(x);
              break;
            }
          }
        }
      } else
        stk.push(a[j]);
      if (stk.size() == 1) dp[i] = min(dp[i], dp[j + 1] + 1);
    }
  }
  cout << dp[1] << endl;
}
int main() {
  c_plus_plus_main();
  return 0;
}
