#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T>
istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  for (int i = 0; i < int(v.size()); ++i)
    os << v[i] << (i == int(v.size() - 1) ? ']' : ',');
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T>
bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
int pos[510][510];
vector<int> a;
int rec(int i, int j) {
  if (pos[i][j] != -1) return pos[i][j];
  if (j - i == 1) return pos[i][j] = a[i];
  for (int k = int(i + 1); k < int(j); ++k) {
    int l = rec(i, k), r = rec(k, j);
    if (l == 0 or r == 0) continue;
    if (l == r) return pos[i][j] = l + 1;
  }
  return pos[i][j] = 0;
}
int dp[510];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  a.resize(n);
  cin >> a;
  for (int i = 0; i < int(510); ++i)
    for (int j = 0; j < int(510); ++j) pos[i][j] = -1;
  for (int i = 0; i < int(n); ++i)
    for (int j = int(i + 1); j < int(n + 1); ++j) {
      if (pos[i][j] == -1) rec(i, j);
    }
  for (int i = 0; i < int(510); ++i) dp[i] = inf;
  dp[0] = 0;
  for (int i = 0; i < int(n); ++i)
    for (int j = int(i + 1); j < int(n + 1); ++j) {
      if (pos[i][j]) chmin(dp[j], dp[i] + 1);
    }
  cout << dp[n] << endl;
}
