#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int n;
int dp[N][N];
int a[N];
int f(int l, int r) {
  if (dp[l][r]) return dp[l][r];
  if (r - l < 1) return 1;
  if (r - l == 1) {
    if (a[l] == a[r])
      return 1;
    else
      return 2;
  }
  if (a[r] == a[l])
    dp[l][r] = f(l + 1, r - 1);
  else {
    int mn = 0x7fffffff;
    for (long long k = l; k <= r - 1; ++k) {
      mn = min(mn, f(l, k) + f(k + 1, r));
    }
    dp[l][r] = mn;
  }
  return dp[l][r];
}
int solve(int T) {
  memset(dp, 0, sizeof(dp));
  cin >> n;
  for (long long i = 1; i <= n; ++i) cin >> a[i];
  memset(dp, 0x3f, sizeof(dp));
  for (long long i = 1; i <= n; ++i) dp[i][i] = 1;
  for (long long i = 1; i <= n - 1; ++i) dp[i][i + 1] = 1 + (a[i] != a[i + 1]);
  for (long long i = 3; i <= n; ++i) {
    for (long long j = 1; j <= n - i + 1; ++j) {
      int l = j, r = j + i - 1;
      if (a[l] == a[r]) dp[l][r] = dp[l + 1][r - 1];
      for (long long k = l; k <= r - 1; ++k)
        dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);
    }
  }
  cout << dp[1][n];
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  solve(T);
  return 0;
}
