// Arup Guha
// 3/6/2024
// Solution to 2023 SER D1 Problem H: Range Editing

import java.util.*;

public class h {

	public static int n;
	public static int[] s;
	public static TreeSet<Integer>[] locs;
	
	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		n = stdin.nextInt();
		HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
		map.put(0,0);
		int id = 1;
		
		ArrayList<Integer> seq = new ArrayList<Integer>();
		for (int i=0; i<n; i++) {
		
			// Read next int.
			int cur = stdin.nextInt();
			
			// Update map if necessary.
			if (!map.containsKey(cur)) map.put(cur, id++);
			
			// Update to mapped int.
			cur = map.get(cur);
			
			// Getting rid of consecutive duplicates.
			if (seq.size() > 0 && cur == seq.get(seq.size()-1)) continue;
			
			// Add it.
			seq.add(cur);
		}
		
		// Update.
		n = seq.size();
		s = new int[n];
		for (int i=0; i<n; i++)
			s[i] = seq.get(i);
		
		// locs[i] is a list storing each location the value i occurs in the zipped input list.
		locs = new TreeSet[id];
		for (int i=0; i<id; i++)
			locs[i] = new TreeSet<Integer>();
		for (int i=0; i<n; i++)
			locs[s[i]].add(i);
		
		// memo table.
		HashMap<Integer,Integer> memo = new HashMap<Integer,Integer>();
		System.out.println(go(0, n-1, 0, memo));
	}
	
	public static int go(int sI, int eI, int col, HashMap<Integer,Integer> memo) {
	
		// Nothing to color.
		if (sI > eI) return 0;
		
		// Does my first color match?
		int curCnt = (col == s[sI]) ? 0 : 1;
		
		// Code for my hashmap.
		int code = col*n*n + n*sI + eI;
		
		// Easy case.
		if (sI == eI) return curCnt;
		
		// We've done this before.
		if (memo.containsKey(code)) return memo.get(code);
		
		// Just color this particular square.
		int res = curCnt + go(sI+1, eI, col, memo);
		
		// Find the next index we find this color.
		Integer nI = locs[s[sI]].higher(sI);
		
		// Color on the bottom all the way to the next spot with this color.
		while (nI != null) {
			
			// Out of our range.
			if (nI > eI) break;
			
			// Costs of both recursive calls.
			int left = curCnt + go(sI+1, nI-1, s[sI], memo);
			int right = go(nI+1, eI, col, memo);
			
			// Update if this is better.
			res = Math.min(res, left+right);
			
			// Go to next index with value s[sI].
			nI = locs[s[sI]].higher(nI);
		}
		
		// Place in map and return.
		memo.put(code, res);
		return res;
	}
}