#include <bits/stdc++.h>
using namespace std;
inline int read() {
  register int x = 0, f = 1;
  register char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = 0;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + (ch ^ '0');
    ch = getchar();
  }
  return f ? x : -x;
}
const int N = 5e2 + 5;
int n, a[N], f[N][N], p[N][N][2];
int main() {
  for (int i = (1), _ed = (n = read()); i <= _ed; ++i) a[i] = read();
  memset(f, 0x3f, sizeof f);
  for (int i = (1), _ed = (n); i <= _ed; ++i)
    f[i][i] = 1, p[i][i][0] = p[i][i][1] = a[i];
  for (int len = (2), _ed = (n); len <= _ed; ++len)
    for (int l = (1), _ed = (n - len + 1); l <= _ed; ++l) {
      int r = l + len - 1;
      for (int i = (l), _ed = (r - 1); i <= _ed; ++i) {
        int tmp = f[l][i] + f[i + 1][r] - (p[l][i][1] == p[i + 1][r][0]);
        if (f[l][r] > tmp) {
          f[l][r] = tmp;
          p[l][r][0] = p[l][i][0], p[l][r][1] = p[i + 1][r][1];
          if (f[l][i] == 1) p[l][r][0] += (p[l][i][1] == p[i + 1][r][0]);
          if (f[i + 1][r] == 1) p[l][r][1] += (p[l][i][1] == p[i + 1][r][0]);
        }
      }
    }
  printf("%d\n", f[1][n]);
  return 0;
}
