#include <bits/stdc++.h>
using namespace std;
int gem[500 + 1];
int dp[500 + 1][500 + 1];
int main(int argc, const char* argv[]) {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &gem[i]);
  }
  for (int i = 1; i <= n; i++) {
    dp[i][i] = 1;
  }
  for (int i = 1; i <= n - 1; i++) {
    dp[i][i + 1] = 1 + (gem[i] != gem[i + 1]);
  }
  for (int h = 2; h < n; h++) {
    for (int i = 1; i <= n - h; i++) {
      int j = i + h;
      dp[i][j] = dp[i + 1][j - 1] + 2 * (gem[i] != gem[j]);
      for (int k = i; k < j; k++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
      }
    }
  }
  printf("%d\n", dp[1][n]);
  return 0;
}
