#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int n;
const int mm = 509;
int dp[mm][mm], in[mm];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> in[i];
  const int inf = 1 << 29;
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= n; i++) dp[i][i] = in[i];
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= n - i + 1; j++) {
      int r = j + i - 1;
      int now = -1;
      for (int k = j; k < r; k++) {
        if (dp[j][k] > 0 and dp[j][k] == dp[k + 1][r]) {
          now = dp[j][k] + 1;
          break;
        }
      }
      dp[j][r] = now;
    }
  }
  vector<int> res(mm, inf);
  res[0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = i; j <= n; j++)
      if (dp[i + 1][j] > 0) res[j] = min(res[j], res[i] + 1);
  }
  cout << res[n] << endl;
}
