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