#include <bits/stdc++.h>
using namespace std;
int n, cnt;
int col[505];
int dp[505][505];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> col[i];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++) {
      if (i == j)
        dp[i][j] = 1;
      else
        dp[i][j] = 99999999;
    }
  for (int l = 1; l <= n; l++) {
    if (col[l] == col[l + 1])
      dp[l][l + 1] = 1;
    else
      dp[l][l + 1] = 2;
  }
  for (int l = 2; l <= n; l++) {
    for (int r = 1; r + l <= n; r++) {
      for (int k = r; k < r + l; k++) {
        dp[r][l + r] = min(dp[r][k] + dp[k + 1][r + l], dp[r][l + r]);
      }
      if (col[r] == col[l + r])
        dp[r][l + r] = min(dp[r + 1][l + r - 1], dp[r][l + r]);
      else
        dp[r][l + r] = min(dp[r][l + r], dp[r + 1][l + r - 1] + 2);
    }
  }
  cout << dp[1][n] << endl;
  return 0;
}
