var n;
var x = [];

function main(){
	n = scan();
	rep(n, function(i){
		x[i] = scan();
	});
	var p = [0, 0, -Infinity, -Infinity, -Infinity, -Infinity];
	var ans = 0;
	rep(n, function(i){
		var a = x[i];
		var b = 1 - a;
		var q = [];
		q[a] = p[b] + 1;
		q[b] = 0;
		q[2 + b] = Math.max(p[a], p[2 + a]) + 1;
		q[2 + a] = 0;
		q[4 + a] = Math.max(p[2 + b], p[4 + b]) + 1;
		q[4 + b] = 0;
		ans = Math.max(Math.max.apply(null, q), ans);
		p = q;
	});
	print(ans);
}
 
function rep(a, b, c){
	if(c === undefined){
		c = b;
		b = a;
		a = 0;
	}
	for(var i = a; i < b; ++i){
		if(c(i) === false){
			break;
		}
	}
}
 
process.stdin.resume();
process.stdin.setEncoding('utf8');
 
var input = '';
var input_index = 0;
 
function scan(type){
	if(type === 'string'){
		return input[input_index++];
	}
	else{
		return +input[input_index++];
	}
}
 
function print(val){
	console.log(val);
}
 
process.stdin.on('data', function(chunk){
	input += chunk;
});
process.stdin.on('end', function(){
	input = input.trim().split(/\s+/);
	main();
});