#include <bits/stdc++.h>
using namespace std;
const int maxn = 505, maxmax = 1000000;
int n, javab[maxn][maxn];
vector<int> v;
int dp(int l, int r) {
  int x = maxmax;
  if (l == r) {
    javab[l][r] = 1;
    return 1;
  }
  if (l == r - 1) {
    if (v[r] == v[l]) {
      javab[l][r] = 1;
      return 1;
    } else {
      javab[l][r] = 2;
      return 2;
    }
  }
  if (javab[l][r - 1] != 0)
    x = min(x, javab[l][r - 1] + 1);
  else {
    x = min(x, dp(l, r - 1) + 1);
  }
  for (int i = r - 1; i >= l; i--) {
    if (v[r] == v[i]) {
      if (i == l) {
        if (javab[l + 1][r - 1] != 0) {
          x = min(x, javab[l + 1][r - 1]);
        } else
          x = min(x, dp(l + 1, r - 1));
        break;
      }
      if (javab[l][i - 1] != 0) {
        if (javab[i][r] != 0) {
          x = min(x, javab[l][i - 1] + javab[i][r]);
        } else {
          x = min(x, javab[l][i - 1] + dp(i, r));
        }
      } else {
        if (javab[i][r] != 0) {
          x = min(x, dp(l, i - 1) + javab[i][r]);
        } else {
          x = min(x, dp(l, i - 1) + dp(i, r));
        }
      }
    }
  }
  javab[l][r] = x;
  return x;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  int javab = dp(0, v.size() - 1);
  cout << javab;
  return 0;
}
