#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=505;
int n, a[maxn], d[maxn][maxn];
void read() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
}
void fun() {
for (int len = 1; len <= n; len++) {
for (int beg = 0, end = len - 1; end < n; beg++, end++) {
if (len == 1) {
d[beg][end] = 1;
} else {
d[beg][end] = 1 + d[beg + 1][end];
if (a[beg] == a[beg + 1]) {
d[beg][end] = min(1 + d[beg + 2][end], d[beg][end]);
}
for (int match = beg + 2; match <= end; match++) {
if (a[beg] == a[match]) {
d[beg][end] = min(d[beg + 1][match - 1] + d[match + 1][end], d[beg][end]);
}
}
}
}
}
}
int main() {
read();
fun();
printf("%d\n", d[0][n-1]);
return 0;
}
I2luY2x1ZGU8YWxnb3JpdGhtPgojaW5jbHVkZTxjc3RkaW8+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBtYXhuPTUwNTsKCmludCBuLCBhW21heG5dLCBkW21heG5dW21heG5dOwoKdm9pZCByZWFkKCkgewoJc2NhbmYoIiVkIiwgJm4pOwoJZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKCQlzY2FuZigiJWQiLCBhICsgaSk7Cgl9Cn0KCnZvaWQgZnVuKCkgewoJZm9yIChpbnQgbGVuID0gMTsgbGVuIDw9IG47IGxlbisrKSB7CgkJZm9yIChpbnQgYmVnID0gMCwgZW5kID0gbGVuIC0gMTsgZW5kIDwgbjsgYmVnKyssIGVuZCsrKSB7CgkJCWlmIChsZW4gPT0gMSkgewoJCQkJZFtiZWddW2VuZF0gPSAxOwoJCQl9IGVsc2UgewoJCQkJZFtiZWddW2VuZF0gPSAxICsgZFtiZWcgKyAxXVtlbmRdOwoJCQkJaWYgKGFbYmVnXSA9PSBhW2JlZyArIDFdKSB7CgkJCQkJZFtiZWddW2VuZF0gPSBtaW4oMSArIGRbYmVnICsgMl1bZW5kXSwgZFtiZWddW2VuZF0pOwoJCQkJfQoJCQkJZm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