#include <bits/stdc++.h>
const int SZ = 5e2 + 5;
using namespace std;
int n, a[SZ], dp[SZ][SZ];
int DP(int i, int j) {
  if (i == j || (j - i == 1 && a[i] == a[j])) return 1;
  if (dp[i][j] != -1) return dp[i][j];
  int c1 = SZ;
  for (int k = i; k < j; k++) c1 = min(c1, DP(i, k) + DP(k + 1, j));
  int c2 = SZ, c3 = SZ;
  if (a[i] == a[j])
    c2 = DP(i + 1, j - 1);
  else
    c3 = 1 + DP(i + 1, j) + DP(i, j - 1);
  return dp[i][j] = min(min(c3, c2), c1);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  memset(dp, -1, sizeof dp);
  cout << DP(0, n - 1);
}
