#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int N;
  cin >> N;
  vector<vector<int>> matrix(N);
  vector<int> colour(N);
  for (int i = 0; i < N; ++i) {
    cin >> colour[i];
  }
  for (int i = 0; i < N; ++i) {
    matrix[i].resize(N);
  }
  for (int i = 0; i < N; ++i) {
    matrix[i][i] = 1;
    for (int j = 0; j < i; ++j) matrix[i][j] = 0;
  }
  for (int l = 1; l <= N - 1; ++l)
    for (int i = 0; i < N - l; ++i) {
      matrix[i][i + l] = 1 + matrix[i + 1][l + i];
      if (i + 1 < N && colour[i] == colour[i + 1]) {
        matrix[i][i + l] =
            min(matrix[i][i + l], 1 + ((i + 2 < N) ? matrix[i + 2][l + i] : 0));
      }
      for (int k = i + 2; k <= i + l; k++)
        if (colour[i] == colour[k]) {
          if (k < N - 1)
            matrix[i][i + l] = min(matrix[i][i + l],
                                   matrix[i + 1][k - 1] + matrix[k + 1][i + l]);
          else
            matrix[i][i + l] = min(matrix[i][i + l], matrix[i + 1][k - 1]);
        }
    }
  cout << matrix[0][N - 1];
  return 0;
}
