#include <bits/stdc++.h>
using namespace std;
int a[550], dp[550][550], sym[550][550];
int issym(int x, int y) {
  while (x < y) {
    if (a[x] != a[y]) return 0;
    x++;
    y--;
  }
  return 1;
}
int DP(int x, int y) {
  if (dp[x][y] >= 0) return dp[x][y];
  if (sym[x][y]) return dp[x][y] = 1;
  dp[x][y] = 1 << 29;
  for (int i = x; i < y; i++) {
    dp[x][y] = min(dp[x][y], DP(x, i) + DP(i + 1, y));
  }
  for (int i = 0; i < (y - x) / 2; i++) {
    if (a[x + i] != a[y - i]) break;
    dp[x][y] = min(dp[x][y], DP(x + i + 1, y - i - 1));
  }
  return dp[x][y];
}
int main() {
  int n, i, j;
  while (cin >> n) {
    for (i = 1; i <= n; i++) scanf("%d", &a[i]);
    memset(sym, 0, sizeof(sym));
    memset(dp, -1, sizeof(dp));
    for (i = 1; i <= n; i++) {
      for (j = i; j <= n; j++) {
        if (issym(i, j)) sym[i][j] = 1;
      }
    }
    cout << DP(1, n) << endl;
  }
}
