#include <bits/stdc++.h>
using namespace std;
const int MAXN = 505;
const int Inf = 1e9;
int a[MAXN], f[MAXN][MAXN][2], n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int len = 1; len <= n; len++)
    for (int l = 1; l + len - 1 <= n; l++) {
      int r = l + len - 1;
      f[l][r][0] = f[l][r][1] = Inf;
      for (int k = l; k < r; k++) {
        if (a[r] != a[k]) continue;
        int p = (k + 1 > r - 1) ? Inf : f[k + 1][r - 1][0],
            q = f[k + 1][r - 1][1] + 1;
        if (k == l) {
          f[l][r][1] = min(f[l][r][1], min(p, q));
          f[l][r][0] = min(f[l][r][0], min(p, q));
        } else {
          f[l][r][1] =
              min(f[l][r][1], min(p, q) + min(f[l][k - 1][0], f[l][k - 1][1]));
          f[l][r][0] =
              min(f[l][r][0], min(p, q) + min(f[l][k - 1][0], f[l][k - 1][1]));
        }
      }
      f[l][r][1] = min(f[l][r][1], min(f[l][r - 1][0], f[l][r - 1][1]) + 1);
      f[l][r][0] = min(f[l][r][0], min(f[l][r - 1][0], f[l][r - 1][1]) + 1);
    }
  printf("%d\n", min(f[1][n][0], f[1][n][1]));
}
