#include <bits/stdc++.h>
using namespace std;
long long int mod = 998244353;
long long int dx[] = {0, 0, 1, -1};
long long int dy[] = {1, -1, 0, 0};
long long int m = 0, n, res, k, ans = 0, nbcycle = 0;
string s2 = "zero", ch = "", s, t, s1 = "";
set<pair<long long int, long long int> > st;
vector<pair<string, long long int> > vm;
vector<pair<long long int, pair<long long int, long long int> > > vvv;
vector<pair<long long int, long long int> > vv, vv2;
vector<long long int> v[300005], v2;
long long int dp[505][505][2], a[5005];
long long int pal[505][505];
long long int solve(long long int i, long long int j, long long int flag) {
  if (i == j) return 1;
  if (i > j) return flag;
  long long int &ret = dp[i][j][flag];
  if (ret != -1) return ret;
  long long int ans = min(1 + solve(i + 1, j, 0), 1 + solve(i, j - 1, 0));
  for (long long int k = i + 1; k <= j; k++) {
    if (a[i] == a[k])
      ans = min(ans, solve(i + 1, k - 1, 1) + solve(k + 1, j, 0));
  }
  return ret = ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long int i = 0, x, y, z, j = 0, q, mm = 0, mx = -1e9, idx = 0, ok = 0,
                l = 0, r = 0, negatif = 0, positif = 0, l1, r1, d;
  set<pair<long long int, long long int> >::iterator it, it1;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    pal[i][i] = 1;
  }
  memset(dp, -1, sizeof(dp));
  cout << solve(1, n, 0);
  return 0;
}
