#include <bits/stdc++.h>
using namespace std;
int arr[510];
int dp[510][510];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i <= n - 1; i++) scanf("%d", &arr[i]);
  for (int l = 1; l <= n; l++) {
    for (int i = 0; i <= n - 1; i++) {
      int j = i + l - 1;
      if (j > n - 1) continue;
      if (i == j) {
        dp[i][j] = 1;
        continue;
      }
      dp[i][j] = dp[i + 1][j] + 1;
      for (int k = i + 2; k <= j; k++)
        if (arr[k] == arr[i])
          dp[i][j] = min(dp[i][j], dp[i + 1][k - 1] + dp[k + 1][j]);
      if (i < n - 1 && arr[i + 1] == arr[i])
        dp[i][j] = min(dp[i][j], dp[i + 2][j] + 1);
    }
  }
  cout << dp[0][n - 1] << endl;
  return 0;
}
