#include <bits/stdc++.h>
using namespace std;
const long long int M = 1000000007;
int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  int dp[n][n];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; i++) dp[i][i] = arr[i];
  for (int ss = 2; ss <= n; ss++) {
    for (int i = 0; i + ss - 1 < n; i++) {
      int j = i + ss - 1;
      int cc = -1;
      for (int k = i; k < j; k++)
        if (dp[i][k] != 0 && dp[i][k] == dp[k + 1][j]) {
          cc = dp[i][k];
          break;
        }
      dp[i][j] = cc + 1;
    }
  }
  int ans[n];
  for (int i = 0; i < n; i++) {
    ans[i] = INT_MAX;
    if (dp[0][i]) ans[i] = 1;
    for (int j = 1; j <= i; j++) {
      if (dp[j][i]) ans[i] = min(ans[i], ans[j - 1] + 1);
    }
  }
  cout << ans[n - 1] << endl;
  return 0;
}
