#include <bits/stdc++.h>
using namespace std;
int ara[504], dp[504][504], cy;
bool check(int i, int j) {
  if (i >= j) return true;
  if (ara[i] != ara[j]) return false;
  return check(i + 1, j - 1);
}
int rec(int i, int j) {
  if (i > j) return 0;
  if (check(i, j)) return 1;
  if (dp[i][j] != -1) return dp[i][j];
  int ret = 10000007;
  for (int l = j; l >= i; l--) {
    if (ara[l] == ara[i]) {
      ret = min(ret, max(rec(i + 1, l - 1), 1) + rec(l + 1, j));
    }
  }
  return dp[i][j] = ret;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int i, j, n, h, m, k, t, ans, a, b;
  while (cin >> n) {
    for (i = 1; i <= n; i++) {
      cin >> ara[i];
    }
    memset(dp, -1, sizeof dp);
    cout << rec(1, n) << endl;
  }
  return 0;
}
