#include <bits/stdc++.h>
using namespace std;
const int N = 510, inf = 100000;
int a[N], f[N][N][2];
int search(int l, int r, int k) {
  if (l == r) return f[l][r][k] = 1;
  if (l == r - 1) {
    if (a[l] == a[r])
      return f[l][r][k] = 1;
    else
      return f[l][r][k] = 2;
  }
  if (f[l][r][k]) return f[l][r][k];
  if (k == 0) {
    if (a[l] != a[r]) return f[l][r][k] = inf;
    f[l][r][k] = min(search(l + 1, r - 1, 0), search(l + 1, r - 1, 1));
    return f[l][r][k];
  }
  f[l][r][k] = inf;
  for (int i = l + 1; i <= r; i++) {
    int t = min(search(i, r, 0), search(i, r, 1));
    t = t + min(search(l, i - 1, 0), search(l, i - 1, 1));
    f[l][r][k] = min(t, f[l][r][k]);
  }
  return f[l][r][k];
}
int n;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int ans = min(search(1, n, 0), search(1, n, 1));
  cout << ans;
}
