#include <bits/stdc++.h>
using namespace std;
int c[505], dp[505][505];
void input() {
  int i, j, k, len, n;
  scanf("%d", &n);
  for ((i) = (1); (i) <= (n); (i)++) scanf("%d", &c[i]);
  for ((i) = (1); (i) <= (n); (i)++) {
    dp[i][i] = 1;
    if (i + 1 <= n) {
      if (c[i] == c[i + 1])
        dp[i][i + 1] = 1;
      else
        dp[i][i + 1] = 2;
    }
  }
  for ((len) = (3); (len) <= (n); (len)++)
    for (i = 1, j = i + len - 1; j <= n; i++, j++) {
      dp[i][j] = len;
      for ((k) = (i); (k) <= (j - 1); (k)++)
        dp[i][j] = ((dp[i][j]) < (dp[i][k] + dp[k + 1][j])
                        ? (dp[i][j])
                        : (dp[i][k] + dp[k + 1][j]));
      if (c[i] == c[j])
        dp[i][j] =
            ((dp[i][j]) < (dp[i + 1][j - 1]) ? (dp[i][j]) : (dp[i + 1][j - 1]));
    }
  printf("%d\n", dp[1][n]);
}
int main() {
  input();
  return 0;
}
