#include <bits/stdc++.h>
using namespace std;
int a[555], f[555][555];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  for (int len = 1; len <= n; ++len) {
    for (int i = 1; i <= n - len + 1; ++i) {
      int j = i + len - 1;
      if (len == 1)
        f[i][j] = 1;
      else if (len == 2) {
        if (a[i] == a[j])
          f[i][j] = 1;
        else
          f[i][j] = 2;
      } else {
        f[i][j] = f[i + 1][j - 1];
        if (a[i] != a[j]) f[i][j] += 2;
      }
      for (int k = i; k <= j - 1; ++k)
        f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j]);
    }
  }
  printf("%d\n", f[1][n]);
}
