#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto& x : a) {
    cin >> x;
  }
  vector<vector<int>> dp(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    dp[i][i] = 1;
  }
  for (int i = 0; i + 1 < n; i++) {
    dp[i][i + 1] = 2;
    if (a[i] == a[i + 1]) {
      dp[i][i + 1] = 1;
    }
  }
  for (int l = 3; l <= n; l++) {
    for (int i = 0; i + l - 1 < n; i++) {
      int j = i + l - 1;
      dp[i][j] = j - i + 1;
      for (int k = i; k < j; k++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
      }
      if (a[i] == a[j]) {
        dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]);
      }
    }
  }
  cout << dp[0][n - 1] << "\n";
}
