#include <bits/stdc++.h>
int n;
int x[510], s[510], nxt[510];
int dp[510][510];
int min(int a, int b) { return a < b ? a : b; }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &s[i]);
  for (int i = 1; i <= n; i++) nxt[x[s[i]]] = i, x[s[i]] = i;
  for (int i = 1; i <= n; i++) dp[i][i] = 1;
  for (int len = 2; len <= n; len++) {
    for (int i = 1; i + len - 1 <= n; i++) {
      int j = i + len - 1;
      dp[i][j] = dp[i + 1][j] + 1;
      int now = i;
      while (x[s[now]] != now && nxt[now] <= j) {
        now = nxt[now];
        dp[i][j] = min(dp[i][j], (now == i + 1 ? 1 : dp[i + 1][now - 1]) +
                                     (now == j ? 0 : dp[now + 1][j]));
      }
    }
  }
  printf("%d\n", dp[1][n]);
}
