#include <bits/stdc++.h>
const double PI = 3.1415926535897932384626433;
const int KL = 505;
const long long MOD = 1e9 + 7;
using namespace std;
int n, dp[KL][KL][2], s[KL];
bool vis[KL][KL][2];
int solve(int st, int en, bool ok) {
  if (st > en) return 0;
  if (vis[st][en][ok]) return dp[st][en][ok];
  vis[st][en][ok] = 1;
  int path = 1e6;
  if (ok) {
    if (s[st] == s[en]) path = solve(st + 1, en - 1, 1);
    for (int i = st + 1; i <= en; i++) {
      if (s[i] == s[en])
        path = min(path, solve(st, i - 1, 0) + solve(i, en, 1));
    }
    for (int i = st; i < en; i++) {
      if (s[st] == s[i])
        path = min(path, solve(st, i, 1) + solve(i + 1, en, 0));
    }
  } else {
    if (s[st] == s[en]) path = 1 + solve(st + 1, en - 1, 1);
    for (int i = st + 1; i <= en; i++) {
      if (s[i] == s[en])
        path = min(path, solve(st, i - 1, 0) + solve(i, en, 0));
    }
    for (int i = st; i < en; i++) {
      if (s[st] == s[i])
        path = min(path, solve(st, i, 0) + solve(i + 1, en, 0));
    }
  }
  return dp[st][en][ok] = path;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &s[i]);
  cout << solve(0, n - 1, 0) << endl;
  return 0;
}
