#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
int s[510];
int f[510][510];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> s[i];
  memset(f, 0x3f, sizeof f);
  for (int i = 1; i <= n; ++i) f[i][i] = 1;
  for (int i = 0; i < 510; ++i)
    for (int j = 0; j < i; ++j) f[i][j] = 0;
  for (int len = 2; len <= n; ++len)
    for (int i = 1; i + len - 1 <= n; ++i) {
      int j = i + len - 1;
      for (int k = i; k <= j; ++k) {
        if (s[i] == s[k])
          f[i][j] =
              min(f[i][j], f[i + 1][k - 1] + f[k + 1][j] + (i + 1 < k ? 0 : 1));
      }
    }
  cout << f[1][n];
  return 0;
}
