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