import java.io.*;
import java.util.*;

public class war_pk {

	PrintWriter out;
	BufferedReader br;
	StringTokenizer st;

	String nextToken() throws IOException {
		while ((st == null) || (!st.hasMoreTokens()))
			st = new StringTokenizer(br.readLine());
		return st.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	public void solve() throws IOException {
		int n = nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		int[] right = new int[n];
		right[n - 1] = n - 1;
		int last = n - 1;
		for (int i = n - 2; i >=0; i--) {
			if (a[i] != a[i + 1])
				last = i;
			right[i] = last;
		}
		
		int[][] dp = new int[n][n];
		for (int l = 1; l <= n; l++) {
			for (int i = 0; i < n - l + 1; i++) {
				dp[i][i + l - 1] = 1;
				if (l > 1)
					dp[i][i + l - 1] += dp[i + 1][i + l - 1];
				if (right[i] >= i + l - 1)
					dp[i][i + l - 1] = 1;
				else {
					for (int k = i + 1; k <= i + l - 1; k++) {
						if (a[k] != a[i])
							continue;
						dp[i][i + l - 1] = Math.min(dp[i][i + l - 1], dp[right[i] + 1][k - 1] + dp[k][i + l - 1]);
					}
				}
			}
		}
		out.println(dp[0][n - 1]);
	}

	public void run() {
		try {
			br = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);

			br = new BufferedReader(new FileReader("war.in"));
			out = new PrintWriter("war.out");

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new war_pk().run();
	}
}
