#include <bits/stdc++.h>
using namespace std;
const int maxn = 501;
int d[maxn][maxn];
int a[maxn][maxn];
int was[maxn][maxn];
int s[maxn];
int g(int i, int j) {
  if (d[i][j]) return d[i][j];
  if (i == j) return d[i][j] = 1;
  if (i + 1 == j) return d[i][j] = 1 + (s[i] != s[j]);
  d[i][j] = j - i + 1;
  if (s[i] == s[j]) {
    g(i + 1, j - 1);
    d[i][j] = d[i + 1][j - 1];
  }
  for (int k = i; k < j; k++) {
    g(i, k);
    g(k + 1, j);
    d[i][j] = min(d[i][j], d[i][k] + d[k + 1][j]);
  }
  return d[i][j];
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i + 1];
  }
  cout << g(1, n) << endl;
  return 0;
}
