#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, e, dp[502][502], i, j, f[502], ii;
bool dpone[502][502];
set<pair<int, int> > s;
set<pair<int, int> >::iterator it, t, tt;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> a;
  for (i = 1; i <= a; i++) cin >> f[i];
  for (i = 1; i <= a; i++) {
    for (j = 1; j <= a; j++) dp[i][j] = a + 2;
  }
  for (i = 1; i <= a; i++) {
    s.clear();
    for (j = i; j <= a; j++) {
      s.insert(make_pair(j, f[j]));
      for (it = s.begin(); it != s.end();) {
        t = it;
        t++;
        if (t == s.end()) break;
        if ((*it).second < (*t).second) break;
        if ((*it).second > (*t).second) {
          it++;
          continue;
        }
        if (it != s.begin()) {
          tt = it;
          tt--;
          s.insert(make_pair((*it).first, (*it).second + 1));
          s.erase(it);
          s.erase(t);
          it = tt;
        } else {
          s.insert(make_pair((*it).first, (*it).second + 1));
          s.erase(it);
          s.erase(t);
          it = s.begin();
        }
      }
      if (s.size() == 1) dpone[i][j] = 1;
    }
  }
  for (ii = 1; ii <= a; ii++) {
    d = a - ii + 1;
    for (i = 1; i <= d; i++) {
      j = i + ii - 1;
      if (dpone[i][j] == 1) {
        dp[i][j] = 1;
        continue;
      }
      for (c = i; c < j; c++) {
        if (dp[i][j] > dp[i][c] + dp[c + 1][j])
          dp[i][j] = dp[i][c] + dp[c + 1][j];
      }
    }
  }
  cout << dp[1][a];
  return 0;
}
