#include <bits/stdc++.h>
using namespace std;
int dp[505][505];
int mat[505];
int min(int a, int b) { return a < b ? a : b; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> mat[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) dp[i][j] = (int)1e9;
  for (int i = 0; i < n; i++) dp[i][i] = 1;
  for (int i = 0; i < n - 1; i++) dp[i][i + 1] = (mat[i] == mat[i + 1] ? 1 : 2);
  for (int diff = 2; diff < n; diff++) {
    for (int i = 0; i + diff < n; i++) {
      int j = i + diff;
      dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
      for (int k = i; k < j; k++) {
        int extra;
        if (mat[k] == mat[j]) {
          extra = 0;
          if (k + 1 == j) extra = 1;
        } else
          extra = 2;
        dp[i][j] =
            min(dp[i][j], (k > i ? dp[i][k - 1] : 0) +
                              (k < j - 1 ? dp[k + 1][j - 1] : 0) + extra);
      }
    }
  }
  cout << dp[0][n - 1] << endl;
}
