#include <bits/stdc++.h>
using namespace std;
const int INF = int(1e9);
const int MOD = int(1e9) + 7;
const long long N = 1000010;
const long long INFll = 1ll * INF * INF;
const long double INFld = 1e+018;
const long double EPS = 0.000000001;
const int sdx[4] = {-1, 0, 1, 0};
const int sdy[4] = {0, 1, 0, -1};
const long double PI = 3.1415926535897932384;
template <typename T>
ostream& operator<<(ostream& out, pair<T, T>& a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T>
istream& operator>>(istream& in, pair<T, T>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T>
istream& operator>>(istream& in, vector<T>& a) {
  for (int i = 0; i < a.size(); ++i) in >> a[i];
  return in;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> a) {
  for (int i = 0; i < a.size(); ++i)
    if (i == a.size() - 1)
      out << a[i];
    else
      out << a[i] << " ";
  return out;
}
int dp[510][510];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) dp[i][i] = 1;
  for (int i = n - 1; i >= 1; i--)
    for (int j = i + 1; j <= n; j++) {
      dp[i][j] = 1 + dp[i + 1][j];
      for (int k = i + 2; k <= j; k++)
        if (a[i] == a[k])
          dp[i][j] = min(dp[i][j], dp[i + 1][k - 1] + dp[k + 1][j]);
      if (a[i] == a[i + 1]) dp[i][j] = min(dp[i][j], 1 + dp[i + 2][j]);
    }
  cout << dp[1][n] << endl;
  if (0)
    cerr << fixed << setprecision(0)
         << "TIME = " << clock() / (long double)CLOCKS_PER_SEC * 1000 << "\n";
  return 0;
}
