#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 50;
int dp[N][N];
int n;
int a[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  memset(dp, 31, sizeof dp);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i <= n; ++i) dp[i][i] = 0, dp[i][i + 1] = 1;
  for (int k = 2; k <= n; ++k) {
    for (int l = 0; l + k <= n; ++l) {
      int r = l + k;
      dp[l][r] = 1 + dp[l + 1][r];
      for (int i = l + 1; i < r; ++i) {
        if (a[l] != a[i]) continue;
        dp[l][r] = min(dp[l][r], max(dp[l + 1][i], 1) + dp[i + 1][r]);
      }
    }
  }
  cout << dp[0][n] << "\n";
  return 0;
}
