#include <fstream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	ifstream cin("war.in");
	ofstream cout("war.out");
	int n;
	cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++)
		cin >> a[i];

	vector<vector<int> > dp(n + 2, vector<int>(n + 2, n));

	for (int i = 0; i <= n + 1; i++)
		dp[i][0] = 0, dp[i][1] = 1;

	for (int len = 2; len <= n; len++)
		for (int i = 0; i < n; i++)
		{
			if (len + i > n) break;
			int l = 0;
			while (l < len && a[i + l] == a[i]) l++;
			dp[i][len] = min(len, 1 + dp[i + l][len - l]);
			for (int len2 = l + 1; len2 <= len; len2++)
				if (a[i + len2 - 1] == a[i])
					dp[i][len] = min(dp[i][len], dp[i + l][len2 - l] + dp[i + len2][len - len2]);
		}
	cout << dp[0][n] << endl;
}
