#include <bits/stdc++.h>
using namespace std;
int dp[600][600], a[600];
const int inf = 2e9;
int main() {
  int n, m, i, j, len, k;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    dp[i][i] = 1;
  }
  for (i = 1; i <= n - 1; i++) {
    if (a[i] == a[i + 1])
      dp[i][i + 1] = 1;
    else
      dp[i][i + 1] = 2;
  }
  for (len = 3; len <= n; len++) {
    for (i = 1; i + len - 1 <= n; i++) {
      j = i + len - 1;
      dp[i][j] = inf;
      dp[i][j] = min(dp[i][j], dp[i + 1][j] + 1);
      if (a[i] == a[i + 1]) {
        dp[i][j] = min(dp[i][j], dp[i + 2][j] + 1);
      }
      if (a[i] == a[j]) {
        dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]);
      }
      for (k = i + 2; k < j; k++) {
        if (a[i] == a[k]) {
          dp[i][j] = min(dp[i][j], dp[i + 1][k - 1] + dp[k + 1][j]);
        }
      }
    }
  }
  printf("%d\n", dp[1][n]);
  return 0;
}
