#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
void file() {}
const int N = 5e2 + 5;
int a[N], dp[N][N], G[N][N];
signed main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  int n;
  cin >> n;
  memset(dp, 0x3f3f3f3f, sizeof(dp));
  ;
  memset(G, 0x3f3f3f3f, sizeof(G));
  ;
  for (int i = 1; i <= n; ++i) cin >> a[i], dp[i][i] = 1, G[i][i] = a[i];
  for (int len = 1; len < n; ++len) {
    for (int i = 1; i + len <= n; ++i) {
      int j = i + len;
      for (int k = i; k < j; ++k) {
        if (dp[i][k] == 1 && dp[k + 1][j] == 1 && G[i][k] == G[k + 1][j])
          dp[i][j] = 1, G[i][j] = G[i][k] + 1;
        else
          dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
      }
      G[i][j] = min(G[i][j], G[i][i] + G[i + 1][j]);
    }
  }
  cout << dp[1][n] << "\n";
  return 0;
}
