#include <bits/stdc++.h>
using namespace std;
int a[505], dp[505][505];
bool palin[505][505];
void solve() {
  int m, n, i, j, k, l, p, q, u, v, w, x, y, z, ii, jj;
  int res = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", a + i);
  for (i = 1; i <= n; i++) {
    dp[i][i] = 1;
  }
  for (i = 1; i < n; i++) {
    if (a[i] == a[i + 1])
      dp[i][i + 1] = 1;
    else
      dp[i][i + 1] = 2;
  }
  for (l = 3; l <= n; l++) {
    for (i = 1; i + l <= n + 1; i++) {
      j = i + l - 1;
      dp[i][j] = 1000000000;
      for (k = i; k < j; k++) {
        dp[i][j] =
            (dp[i][j] < dp[i][k] + dp[k + 1][j] ? dp[i][j]
                                                : dp[i][k] + dp[k + 1][j]);
      }
      if (a[i] == a[j])
        dp[i][j] = (dp[i][j] < dp[i + 1][j - 1] ? dp[i][j] : dp[i + 1][j - 1]);
    }
  }
  printf("%d\n", dp[1][n]);
}
int main() {
  int i, T = 1;
  for (i = 0; i < T; i++) {
    solve();
  }
  return 0;
}
