#include <bits/stdc++.h>
using namespace std;
int dp[510][510], a[510];
int dfs(int l, int r) {
  if (dp[l][r] != -1) return dp[l][r];
  if (l > r) return 0;
  if (l == r) return 1;
  int ret = r - l + 1, left = l, right = r;
  while (left <= right) {
    if (a[left] != a[right]) break;
    left++;
    right--;
    if (left <= right)
      ret = min(ret, dfs(left, right));
    else
      ret = 1;
  }
  for (int i = l; i < r; i++) {
    ret = min(ret, dfs(l, i) + dfs(i + 1, r));
  }
  return dp[l][r] = ret;
}
int main() {
  memset(dp, -1, sizeof(dp));
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  printf("%d\n", dfs(1, n));
  return 0;
}
