#include <bits/stdc++.h>
using namespace std;
string sss = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
long long arr[509];
long long dp[509][509];
bool ispal(long long lf, long long rg) {
  if (lf >= rg) return 1;
  if (arr[lf] != arr[rg]) return 0;
  return ispal(lf + 1, rg - 1);
}
long long go(long long lf, long long rg) {
  if (lf > rg) return 0;
  if (ispal(lf, rg)) return dp[lf][rg] = 1;
  if (dp[lf][rg] != -1) return dp[lf][rg];
  long long ret = 10000000;
  for (int i = rg; i >= lf; i--) {
    if (arr[i] == arr[lf])
      ret = min(ret, max(1LL, go(lf + 1, i - 1)) + go(i + 1, rg));
  }
  return dp[lf][rg] = ret;
}
void solve(long long CS) {
  memset(dp, -1, sizeof(dp));
  long long n, i, j;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> arr[i];
  cout << go(1, n) << endl;
}
int main() {
  long long t, no = 0;
  t = 1;
  while (t--) solve(++no);
}
