#include <bits/stdc++.h>
using namespace std;
long long d[1000][1001], x[1000][1001], D[1000];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    d[i][1] = 1;
    cin >> x[i][1];
  }
  for (long long l = 2; l <= n; l++) {
    for (long long i = 0; l + i <= n; i++) {
      for (long long j = 1; j + 1 <= l; j++) {
        if (d[i][j] && d[i + j][l - j] && x[i][j] == x[i + j][l - j]) {
          x[i][l] = x[i][j] + 1;
          d[i][l] = 1;
          break;
        }
      }
    }
  }
  for (long long i = 0; i < n; i++) {
    D[i] = 1e9;
    for (long long j = -1; j < i; j++) {
      if (d[j + 1][i - j]) D[i] = min(D[i], (j == -1 ? 0 : D[j]) + 1);
    }
  }
  cout << D[n - 1];
  return 0;
}
