#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
int fin[505][505];
int dp[505][505];
int main() {
  cin.tie(0), ios::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) fin[i][i] = a[i];
  for (int len = 2; len <= n; len++) {
    for (int i = 0; i + len <= n; i++) {
      fin[i][i + len - 1] = -1;
      for (int j = i; j < i + len - 1; j++) {
        if (fin[i][j] != -1 && fin[j + 1][i + len - 1] != -1 &&
            fin[i][j] == fin[j + 1][i + len - 1]) {
          fin[i][i + len - 1] = fin[i][j] + 1;
          break;
        }
      }
    }
  }
  for (int len = 1; len <= n; len++) {
    for (int i = 0; i + len <= n; i++) {
      if (fin[i][i + len - 1] != -1)
        dp[i][i + len - 1] = 1;
      else {
        dp[i][i + len - 1] = len;
        for (int j = i; j < i + len - 1; j++)
          dp[i][i + len - 1] =
              min(dp[i][i + len - 1], dp[i][j] + dp[j + 1][i + len - 1]);
      }
    }
  }
  cout << dp[0][n - 1] << "\n";
  return 0;
}
