#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n;
  cin >> n;
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) cin >> v[i];
  vector<vector<long long> > dp(n, vector<long long>(n, -1));
  for (long long i = 0; i < n; i++) dp[i][i] = v[i];
  for (long long len = 2; len <= n; len++)
    for (long long i = 0; i <= n - len; i++) {
      long long j = i + len - 1;
      for (long long k = i; k < j; k++)
        if (dp[i][k] == dp[k + 1][j] && dp[i][k] != -1) dp[i][j] = dp[i][k] + 1;
    }
  vector<long long> dp2(n + 1, 1e9);
  dp2[0] = 0;
  for (long long i = 1; i <= n; i++)
    for (long long j = 0; j < i; j++)
      if (dp[j][i - 1] != -1) dp2[i] = min(dp2[i], dp2[j] + 1);
  cout << dp2[n] << '\n';
}
