#include <bits/stdc++.h>
using namespace std;
const int MAX = 500 + 9;
int dp[MAX + 9][MAX + 9];
vector<int> v;
bool check(int i, int j) {
  for (int i1 = 0; i1 < j - i; i1++) {
    if (v[i + i1] != v[j - i1]) return false;
  }
  return true;
}
int f(int i, int j) {
  if (i > j) return 0;
  if (check(i, j) == true) return 1;
  if (dp[i][j] != -1) return dp[i][j];
  int value = 1e9;
  for (int i1 = i; i1 <= j; i1++) {
    if (v[i1] == v[i]) {
      value = min(value, max(1, f(i + 1, i1 - 1)) + f(i1 + 1, j));
    }
  }
  return dp[i][j] = value;
}
int main(void) {
  memset(dp, -1, sizeof(dp));
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int in;
    scanf("%d", &in);
    v.push_back(in);
  }
  printf("%d\n", f(0, n - 1));
  return 0;
}
