#include <bits/stdc++.h>
using namespace std;
int dp[510][510], a[510];
vector<int> v[510];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    v[a[i]].push_back(i);
    dp[i][i] = 1;
  }
  for (int i = 0; i < n - 1; i++) {
    if (a[i] == a[i + 1])
      dp[i][i + 1] = 1;
    else
      dp[i][i + 1] = 2;
  }
  for (int j = 2; j < n; j++) {
    for (int i = 0; i < n; i++) {
      if (i + j > n - 1) break;
      int k = a[i + j];
      dp[i][i + j] = dp[i][i + j - 1] + 1;
      for (int t = 0; t < v[k].size(); t++) {
        if (v[k][t] >= i && v[k][t] < i + j) {
          int z = 0;
          if (v[k][t] - 1 >= i) z += dp[i][v[k][t] - 1];
          if (v[k][t] + 1 < i + j - 1)
            z += dp[v[k][t] + 1][i + j - 1];
          else
            z++;
          dp[i][i + j] = min(dp[i][i + j], z);
        }
      }
    }
  }
  cout << dp[0][n - 1];
}
