#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 10;
int n, f[N];
int dp[N][N];
void init() {
  cin >> n;
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= n; i++) {
    scanf("%d", &f[i]);
    dp[i][i] = 1;
  }
}
void work() {
  for (int i = 1; i < n; i++)
    for (int j = 1; j + i <= n; j++) {
      dp[j][j + i] = i + 1;
      if (f[j] == f[j + i]) {
        dp[j][j + i] = dp[j + 1][j + i - 1];
        if (i == 1) dp[j][j + i]++;
      }
      for (int k = j; k < j + i; k++)
        dp[j][j + i] = min(dp[j][j + i], dp[j][k] + dp[k + 1][j + i]);
    }
  cout << dp[1][n] << endl;
}
int main() {
  init();
  work();
  return 0;
}
