#include <bits/stdc++.h>
using namespace std;
int n, a[505], dp[505][505], mark[505][505];
int f(int l, int r) {
  if (mark[l][r] == 1) return dp[l][r];
  if (l <= r) {
    mark[l][r] = 1;
    if (l == r) {
      dp[l][r] = 1;
      return 1;
    }
    if (a[l] == a[r] && l + 1 == r) {
      dp[l][r] = 1;
      return 1;
    }
    if (a[l] == a[l + 1]) {
      dp[l][r] = min(f(l + 2, r) + 1, dp[l][r]);
    }
    int x1, x2;
    for (int k = l; k < r; k++) {
      x1 = f(l, k);
      x2 = f(k + 1, r);
      dp[l][r] = min(dp[l][r], x1 + x2);
    }
    int x3;
    if (a[l] == a[r]) {
      x3 = f(l + 1, r - 1);
      dp[l][r] = min(dp[l][r], x3);
    }
    return dp[l][r];
  } else
    return dp[l][r] = 0;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      mark[i][j] = 0;
      dp[i][j] = 10000000;
    }
  }
  cout << f(1, n);
}
