#include <bits/stdc++.h>
using namespace std;
int n, a[510], dp[510][510];
int main() {
  ios::sync_with_stdio(0);
  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++) {
      dp[i][j] = 100000;
    }
  }
  for (int i = 1; i <= n; i++) {
    dp[i][i] = 1;
  }
  for (int i = 1; i < n; i++) {
    dp[i][i + 1] = 1 + (a[i] != a[i + 1]);
  }
  for (int i = 3; i <= n; i++) {
    for (int j = 1; j + i - 1 <= n; j++) {
      int l = j, r = i + j - 1;
      if (a[l] == a[r]) {
        dp[l][r] = dp[l + 1][r - 1];
      }
      for (int k = l; k < r; k++) {
        dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);
      }
    }
  }
  cout << dp[1][n];
  return 0;
}
