#include <bits/stdc++.h>
using namespace std;
int s[505] = {0};
int ans[505][505] = {0};
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
  for (int i = 0; i < n; ++i) {
    ans[1][i] = 1;
  }
  for (int i = 1; i < n; ++i) {
    if (s[i] == s[i - 1]) {
      ans[2][i - 1] = 1;
    } else {
      ans[2][i - 1] = 2;
    }
  }
  for (int len = 3; len <= n; ++len) {
    for (int i = 0; i + len <= n; ++i) {
      int res = 0x7fffffff;
      for (int j = i + 1; j < i + len; ++j) {
        res = min(res, ans[j - i][i] + ans[i + len - j][j]);
      }
      if (s[i] == s[i + len - 1]) {
        res = min(res, ans[len - 2][i + 1]);
      } else {
        res = min(res, ans[len - 2][i + 1] + 2);
      }
      ans[len][i] = res;
    }
  }
  cout << ans[n][0] << endl;
}
