#include <bits/stdc++.h>
using namespace std;
const int N = 501;
int c[N];
int n;
int dp[N][N];
int solve(int l, int r) {
  if (dp[l][r] != -1) return dp[l][r];
  if (l > r) return dp[l][r] = 0;
  if (l == r) return dp[l][r] = 1;
  dp[l][r] = solve(l + 1, r) + 1;
  if (c[l] == c[l + 1]) dp[l][r] = min(dp[l][r], 1 + solve(l + 2, r));
  for (int k = l + 2; k <= r; ++k) {
    if (c[l] == c[k])
      dp[l][r] = min(dp[l][r], solve(l + 1, k - 1) + solve(k + 1, r));
  }
  return dp[l][r];
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &c[i]);
  memset(dp, -1, sizeof(dp));
  printf("%d\n", solve(0, n - 1));
  return 0;
}
