#include <bits/stdc++.h>
using namespace std;
bool dp1[502][502];
int dp2[502][502];
int func(int *a, int n) {
  for (int i = 1; i <= n; i++) {
    dp1[i][i] = true;
    dp2[i][i] = 1;
  }
  for (int i = 1; i < n; i++) {
    dp1[i][i + 1] = (a[i] == a[i + 1]);
    dp2[i][i + 1] = 1 + (a[i] != a[i + 1]);
  }
  for (int l = 3; l <= n; l++)
    for (int i = 1; i <= n - l + 1; i++) {
      dp1[i][i + l - 1] = dp1[i + 1][i + l - 2] & (a[i] == a[i + l - 1]);
      int j = i + l - 1;
      dp2[i][j] = 1000000000;
      if (dp1[i][j])
        dp2[i][j] = 1;
      else {
        for (int k = i; k < j; k++)
          dp2[i][j] = min(dp2[i][k] + dp2[k + 1][j], dp2[i][j]);
        if (a[i] == a[j]) dp2[i][j] = min(dp2[i][j], dp2[i + 1][j - 1]);
      }
    }
  return dp2[1][n];
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) cin >> a[i];
  cout << func(a, n) << endl;
  return 0;
}
