#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T abs(T a) {
  return ((a < 0) ? -a : a);
}
template <typename T>
inline T sqr(T a) {
  return a * a;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
bool solve(int);
int main() {
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(3);
  int test = 0;
  while (solve(test)) {
    ++test;
  }
  return 0;
}
const int N = 555;
int a[N];
int n;
int z[N][N][2];
int calc(int i, int j, bool cut) {
  if (j - i == 0) {
    return int(cut);
  }
  if (j - i == 1) return 1;
  int& res = z[i][j][cut];
  if (res == -1) {
    res = j - i;
    if (a[i] == a[j - 1]) res = min(res, calc(i + 1, j - 1, true));
    for (int t = int(i + 1); t < int(int(j) + 1); ++t) {
      res = min(res, calc(i, t, false) + calc(t, j, cut));
      res = min(res, calc(i, t, cut) + calc(t, j, false));
    }
  }
  return res;
}
bool solve(int) {
  if (scanf("%d", &n) != 1) return false;
  for (int i = int(0); i < int(n); ++i) assert(scanf("%d", &a[i]) == 1);
  memset(z, -1, sizeof(z));
  printf("%d\n", calc(0, n, false));
  return true;
}
