#include <bits/stdc++.h>
using namespace std;
int N, c;
int memo[505][505], l[505], nextSame[505];
map<int, int> nextSameMap;
int dp(int start, int end) {
  if (end < start) {
    return 0;
  }
  if (start == end) {
    return 1;
  }
  if (memo[start][end] != -1) return memo[start][end];
  int minRes = INT_MAX, idx = start;
  while (idx <= end) {
    int before = dp(start + 1, idx - 1);
    minRes = min(minRes, (before == 0 ? 1 : before) + dp(idx + 1, end));
    idx = nextSame[idx];
  }
  memo[start][end] = minRes;
  return minRes;
}
int main(int argc, const char* argv[]) {
  cin >> N;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) memo[i][j] = -1;
  for (int i = 0; i < N; i++) {
    cin >> l[i];
    nextSameMap[l[i]] = N;
  }
  for (int i = N - 1; i >= 0; i--) {
    nextSame[i] = nextSameMap[l[i]];
    nextSameMap[l[i]] = i;
  }
  cout << dp(0, N - 1) << endl;
  return 0;
}
