// Arup Guha
// 12/13/2017
// Solution to 2017 January USACO Platinum Problem: Subsequence Reversal

import java.util.*;
import java.io.*;

public class subrev {

	public static int n;
	public static int[] a;
	public static int[][][][] dp;

	public static void main(String[] args) throws Exception {

		// Read the grid.
		Scanner stdin = new Scanner(new File("subrev.in"));
		n = stdin.nextInt();
		a = new int[n];
		for (int i=0; i<n; i++)
			a[i] = stdin.nextInt()-1;

		// Store recursive call results here.
		dp = new int[n][n][50][50];
		for (int i=0; i<n; i++)
			for (int j=0; j<n; j++)
				for (int k=0; k<50; k++)
					Arrays.fill(dp[i][j][k], -1);

		// Call and output.
		PrintWriter out = new PrintWriter(new FileWriter("subrev.out"));
		out.println(go(0, n-1, 0, 49));
		out.close();
		stdin.close();
	}

	// Returns the longest increasing subsequence within vals[i..j] that
	// has numbers entirely in between low and high, inclusive.
	public static int go(int i, int j, int low, int high) {

		// Some base cases.
		if (i > j) return 0;
		if (low > high) return 0;
		if (i == j) return low <= a[i] && a[i] <= high ? 1 : 0;
		if (dp[i][j][low][high] != -1) return dp[i][j][low][high];

		int res = Math.max(go(i+1,j,low,high), go(i,j-1,low,high));

		// Don't swap first item in range and take it.
		if (a[i] >= low && a[i] <= high)
			res = Math.max(res, 1 + go(i+1, j, a[i], high));

		// Don't swap last item in range.
		if (a[j] <= high && a[j] >= low)
			res = Math.max(res, 1 + go(i, j-1, low, a[j]));

		// Swapping a[i], a[j] and taking both.
		if (a[j] <= a[i] && a[j] >= low && a[i] <= high)
			res = Math.max(res, 2 + go(i+1, j-1, a[j], a[i]));

		// Swapping a[i], a[j] and taking left value(a[j]) only.
		if (a[j] >= low && a[j] <= high)
			res = Math.max(res, 1 + go(i+1, j-1, a[j], high));

		// Swapping a[i], a[j] and taking right value(a[i]) only.
		if (a[i] <= high && a[i] >= low)
			res = Math.max(res, 1 + go(i+1, j-1, low, a[i]));

		// Ta da!
		return dp[i][j][low][high] = res;
	}
}