// Arup Guha
// 7/3/2013
// Solution to 2013 World Finals Problem H: Marreshka Dolls

import java.util.*;

public class dolls {

	public static int[] array;
	public static int[] memo;
	public static int[][] memoCase;
	public static int[][] minRange;
	public static int[][] cumFreq;

	public static int MAX = 100000000;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		array = new int[n];

		for (int i=0; i<n; i++)
			array[i] = stdin.nextInt();

		memo = new int[n];
		Arrays.fill(memo, -1);

		memoCase = new int[n][n];
		for (int i=0; i<n; i++) {
			Arrays.fill(memoCase[i], -1);
			memoCase[i][i] = 0;
		}

		// Store all mins.
		minRange = new int[n][n];
		for (int i=0; i<n; i++) {
			int tempMin = array[i];
			for (int j=i; j<n; j++) {
				if (array[j] < tempMin) tempMin = array[j];
				minRange[i][j] = tempMin;
			}
		}

		// First we store regular frequencies in here.
		cumFreq = new int[n][501];
		for (int i=0; i<n; i++)
			for (int j=i; j<n; j++)
				cumFreq[i][array[j]]++;

		// Now, calculate cumulative frequencies.
		for (int i=0; i<n; i++)
			for (int j=1; j<501; j++)
				cumFreq[i][j] += cumFreq[i][j-1];

		// Solve.
		int ans = f(n-1);

		// Print result.
		if (ans < MAX)
			System.out.println(ans);
		else
			System.out.println("impossible");
	}

	public static int f(int n) {

		// Base cases.
		if (n == -1) return 0;
		if (memo[n] != -1) return memo[n];

		// Set up.
		int ans = MAX;
		int saven = n, cnt = 0, max = array[n];
		int[] myfreq = new int[501];

		// Loop until it's impossible for this contiguous subsequence to be one doll.
		while (true) {

			if (n < 0) break;

			// Update frequency and max.
			myfreq[array[n]]++;
			if (array[n] > max) max = array[n];
			cnt++;

			// Can't be one doll.
			if (myfreq[array[n]] > 1) break;

			// Update answer.
			if (max == cnt) {
				int temp = solve(n, saven) + f(n-1);
				if (temp < ans)
					ans = temp;
			}

			n--;
		}

		// Store and return.
		memo[saven] = ans;
		return ans;
	}

	public static int solve(int start, int end) {

		if (memoCase[start][end] != -1)
			return memoCase[start][end];

		int best = MAX;

		// Try all split points.
		for (int split=start; split<end; split++) {

			// Evaluate this split point.
			int temp = solve(start, split) + solve(split+1, end) + combine(start, split, end);

			// Update, if necessary.
			if (temp < best)
				best = temp;
		}

		// Store and return.
		memoCase[start][end] = best;
		return best;
	}

	// Returns the cost of combining lists array[start...split] with array[split+1...end].
	public static int combine(int start, int split, int end) {

		// Find which side has the smallest dolls.
		int minLeft = minRange[start][split];
		int minRight = minRange[split+1][end];

		// Use the cumulative frequencies to determine how many dolls are smaller on one
		// side than the smallest on the other side.
		int cnt = 0;
		if (minLeft < minRight) {
			cnt = cumFreq[start][minRight-1] - cumFreq[split+1][minRight-1];
		}
		else {
			if (end+1 < array.length)
				cnt = cumFreq[split+1][minLeft-1] - cumFreq[end+1][minLeft-1];
			else
				cnt = cumFreq[split+1][minLeft-1];
		}

		//  This is how many moves we need here...
		return end - start + 1 - cnt;
	}
}