#include <bits/stdc++.h>
using namespace std;
int INT_MAX_VAL = (int)0x3F3F3F3F;
int INT_MIN_VAL = (int)-0x3F3F3F3F;
long long LONG_MAX_VAL = (long long)0x3F3F3F3F3F3F3F3F;
long long LONG_MIN_VAL = (long long)-0x3F3F3F3F3F3F3F3F;
int ms[506];
int n;
int dp[506][506];
int solve(int l, int r) {
  if (l > r) return 0;
  if (l == r) return 1;
  if (ms[l] == ms[r] && (l + 1) == r) return 1;
  int &res = dp[l][r];
  if (res != -1) return res;
  res = solve(l + 1, r) + 1;
  if (ms[l] == ms[l + 1]) {
    res = min(res, solve(l + 2, r) + 1);
  }
  for (int i = l + 2; i <= r; ++i) {
    if (ms[l] == ms[i]) {
      res = min(res, solve(l + 1, i - 1) + solve(i + 1, r));
    }
  }
  return res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  for (long long(i) = 0LL; (i) < (506); (i)++)
    for (long long(j) = 0LL; (j) < (506); (j)++) dp[i][j] = -1;
  cin >> n;
  for (long long(i) = 0LL; (i) < (n); (i)++) cin >> ms[i];
  cout << solve(0, n - 1) << '\n';
  return 0;
}
