#include <bits/stdc++.h>
using namespace std;
const int N = 512, lgN = 22, mod = 1000000007;
const double eps = 1e-9, pi = acos(-1.0);
int dp[N][N], c[N];
int main() {
  int n;
  cin >> n;
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= n; ++i) cin >> c[i];
  for (int i = 1; i <= n; ++i) dp[i][i] = 1;
  for (int i = 1; i < n; ++i)
    if (c[i] == c[i + 1])
      dp[i][i + 1] = 1;
    else
      dp[i][i + 1] = 2;
  for (int l = 2; l < n; ++l) {
    for (int i = 1; i + l <= n; ++i) {
      int j = i + l;
      if (c[i] == c[j])
        dp[i][j] = dp[i + 1][j - 1];
      else
        dp[i][j] = mod;
      for (int k = i; k < j; ++k)
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]);
    }
  }
  printf("%d\n", dp[1][n]);
  return 0;
}
