#include <bits/stdc++.h>
using namespace std;
int a[505], flag[505][505], ans[505];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ans[i] = INT_MAX;
    flag[i][i] = a[i];
  }
  for (int len = 2; len <= n; len++) {
    for (int start = 1; start + len - 1 <= n; start++) {
      int last = start + len - 1;
      for (int itr = start; itr < last; itr++) {
        if (flag[start][itr] && flag[start][itr] == flag[itr + 1][last])
          flag[start][last] = flag[start][itr] + 1;
      }
    }
  }
  for (int idx = 1; idx <= n; idx++) {
    for (int itr = 0; itr < idx; itr++) {
      if (flag[itr + 1][idx]) ans[idx] = min(ans[idx], ans[itr] + 1);
    }
  }
  cout << ans[n];
}
