#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int dp[N][N];
int v[N];
int solve(int n) {
  memset(dp, 0x3f, sizeof(dp));
  if (n == 1) return 1;
  if (n == 2) return 1 + (v[0] != v[1]);
  for (int i = 0; i < (n); ++i) dp[i][i] = 1;
  for (int i = 0; i < (n - 1); ++i) dp[i][i + 1] = 1 + (v[i] != v[i + 1]);
  for (int len = (2); len <= (n); ++len)
    for (int i = 0; i < (n - len); ++i) {
      dp[i][i + len] = dp[i + 1][i + len - 1] + (v[i] == v[i + len] ? 0 : 2);
      for (int j = (i); j <= (i + len - 1); ++j)
        dp[i][i + len] = min(dp[i][i + len], dp[i][j] + dp[j + 1][i + len]);
    }
  return dp[0][n - 1];
}
int main() {
  int n;
  while (cin >> n) {
    for (int i = 0; i < (n); ++i) scanf("%d", &v[i]);
    cout << solve(n) << endl;
  }
  return 0;
}
