#include <bits/stdc++.h>
using namespace std;
const int N = 510;
long long int a[N], m[N], valid[N][N], dp[N][N];
vector<int> e[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    valid[1][i] = a[i];
    dp[1][i] = 1;
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= n - i + 1; j++) {
      dp[i][j] = i;
      for (int k = 1; k < i; k++) {
        if (valid[k][j] == valid[i - k][k + j] and valid[k][j] != 0) {
          valid[i][j] = valid[k][j] + 1;
          dp[i][j] = 1;
        }
        dp[i][j] = min(dp[k][j] + dp[i - k][k + j], dp[i][j]);
      }
    }
  }
  cout << dp[n][1] << "\n";
}
