#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return f * x;
}
const long long N = 7e2 + 7;
const long long M = 1e9 + 7;
const long long MAXN = 2e18 + 7;
const long long Mod = 1004535809;
long long _, i, j, k, n, m, p, q, s, T, t, l, r, o, u, v, w, x, y, z, ans, nex,
    sum, num, len, st, en, sx, sy, tx, ty, th, ma, mi, mod, op, res, flag, cas,
    ret, mid, cnt, now, tmp, rt;
long long a[N], b[N], s1[N], dp[N][N];
char ch;
vector<long long> g[N], h;
string s2, s3;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long T = 1;
  while (T--) {
    n = read();
    for (i = 1; i <= n; i++) s1[i - 1] = read();
    len = n;
    for (i = 0; i < len; i++) {
      dp[i][i] = 1;
    }
    for (i = 0; i < len - 1; i++) {
      if (s1[i] == s1[i + 1])
        dp[i][i + 1] = 1;
      else
        dp[i][i + 1] = 2;
    }
    for (i = 3; i <= len; i++) {
      for (l = 0; l <= len - i; l++) {
        r = l + i - 1;
        if (s1[l] == s1[r])
          dp[l][r] = dp[l + 1][r - 1];
        else
          dp[l][r] = (~(1 << 31));
        for (j = l; j < r; j++) {
          dp[l][r] = min(dp[l][r], dp[l][j] + dp[j + 1][r]);
        }
      }
    }
    printf("%lld\n", dp[0][len - 1]);
  }
  return 0;
}
