#include <bits/stdc++.h>
using namespace std;
const int MAXN = 555;
vector<int> s;
int dp[MAXN][MAXN], is_palin[MAXN][MAXN];
vector<int> substr(int l, int r) {
  vector<int> ans;
  for (int i = l; i <= r; i++) ans.push_back(s[i]);
  return ans;
}
bool is(int l, int r) {
  vector<int> ans = substr(l, r);
  for (int i = 0; i < ans.size() / 2; i++) {
    if (ans[i] != ans[ans.size() - i - 1]) return 0;
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int ch;
  for (int i = 0; i < n; i++) {
    cin >> ch;
    s.push_back(ch);
  }
  for (int i = 0; i < n; i++) dp[i][i] = 1;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == s[i + 1])
      dp[i][i + 1] = 1;
    else
      dp[i][i + 1] = 2;
  }
  for (int l = 3; l <= n; l++) {
    for (int i = 0; i + l - 1 < n; i++) {
      dp[i][i + l - 1] = 1e+9;
      int j = i + l - 1;
      if (is(i, j)) {
        dp[i][j] = 1;
        continue;
      }
      if (s[i] == s[j]) dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]);
      dp[i][j] = min(dp[i][j], 1 + dp[i + 1][j]);
      dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
      for (int k = i + 1; k < j + 1; k++) {
        dp[i][j] = min(dp[i][j], dp[i][k - 1] + dp[k][j]);
      }
    }
  }
  cout << dp[0][n - 1];
}
