#include <bits/stdc++.h>
using namespace std;
vector<int> nxt[505];
int dp[505][505], arr[505];
bool visited[505][505];
int solve(int i, int j) {
  if (i > j) return 0;
  if (j - i < 2) {
    if (arr[i] == arr[j]) return 1;
  }
  if (visited[i][j]) return dp[i][j];
  int temp = 505;
  for (int k = i; k < j; k++) {
    temp = min(temp, solve(i, k) + solve(k + 1, j));
  }
  if (arr[i] == arr[j]) {
    temp = min(temp, solve(i + 1, j - 1));
  }
  dp[i][j] = temp;
  visited[i][j] = true;
  return temp;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  int ans = 505;
  for (int i = 1; i <= n; i++) ans = min(ans, solve(1, i) + solve(i + 1, n));
  cout << ans << endl;
  return 0;
}
