#include <bits/stdc++.h>
using namespace std;
template <class T1>
void deb(T1 e1) {
  cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
  cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
  cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(T1 e1, T2 e2, T3 e3, T4 e4) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl;
}
template <class T1, class T2, class T3, class T4, class T5>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
  cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
       << endl;
}
int n, arr[500 + 7], dp[500 + 7][500 + 7][2];
void reset();
int rec(int left, int right, bool pre);
int main() {
  int i, j, k, res;
  while (scanf("%d", &n) == 1) {
    for (i = 1; i <= n; i++) {
      scanf("%d", arr + i);
    }
    reset();
    res = rec(1, n, 0);
    printf("%d\n", res);
  }
  return 0;
}
void reset() { memset((dp), -1, sizeof(dp)); }
int rec(int left, int right, bool pre) {
  if (left >= right) {
    return 1;
  }
  int &rt = dp[left][right][pre];
  if (rt != -1) {
    return rt;
  }
  int a, b, i, j;
  a = b = 10000;
  if (arr[left] == arr[right]) {
    a = rec(left + 1, right - 1, 1);
  }
  for (i = left; i < right; i++) {
    b = min(b, (pre >= 1) + rec(left, i, 0) + rec(i + 1, right, 0));
    if (pre) {
      b = min(b, rec(left, i, 1) + rec(i + 1, right, 0));
      b = min(b, rec(left, i, 0) + rec(i + 1, right, 1));
    }
  }
  return rt = min(a, b);
}
