#include <bits/stdc++.h>
using namespace std;
int const lim = 500;
int memo[lim + 3][lim + 3][2];
int inp[lim + 3];
int solve(int i, int j, bool pal) {
  int &ret = memo[i][j][pal];
  if (ret != -1) return ret;
  ret = INT_MAX / 2;
  if (i > j) return ret = 0;
  if (pal) {
    if (i == j) return ret = 0;
    if (inp[i] == inp[j]) ret = solve(i + 1, j - 1, pal);
    for (int ii = i; ii < j; ii++) {
      int val1 = solve(i, ii, 0);
      int val2 = solve(ii + 1, j, 1);
      ret = min(ret, val1 + val2);
      val1 = solve(i, ii, 1);
      val2 = solve(ii + 1, j, 0);
      ret = min(ret, val1 + val2);
    }
    return ret;
  }
  if (inp[i] == inp[j]) {
    ret = solve(i + 1, j - 1, 1) + 1;
    ret = min(ret, solve(i + 1, j - 1, 0) + 1);
  }
  for (int ii = i; ii < j; ii++)
    ret = min(ret, solve(i, ii, 0) + solve(ii + 1, j, 0));
  return ret;
}
int main() {
  memset(memo, -1, sizeof memo);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) scanf("%d", &inp[i]);
  cout << min(solve(0, n - 1, 1) + 1, solve(0, n - 1, 0)) << endl;
}
