function Main(input) {
    input = input.trim().split("\n").map(function(x) { return x.split(" ")});    
	let ll = 0;
	while (true){
		if(input[ll][0] === '0') break; 
		var n = parseInt(input[ll][0],10);
		var v_w = input[ll + 1].map(e => parseInt(e, 10));
		dp = [];
		for (var i = 0; i < n; i++){
			dp.push([]);
			for (var j = 0; j < n; j++){
				dp[i].push(-1);
			}
		}
		console.log(kukanDP(v_w, 0, n - 1));
		// console.log(dp);

		ll += 2;
	}
}

function kukanDP(w, l, r){
	// console.log(dp);
	// console.log("lr", l, r, dp);
	// console.log(dp);
    //既に計算済み？
    if(dp[l][r] != -1) return dp[l][r];

    //これ以上取り除けない？
    if(Math.abs(l - r) < 1) {
		// console.log("1",l,r, dp);
		return dp[l][r] = 0;
	}

	if(Math.abs(l - r) === 1 && Math.abs(w[l] - w[r]) <= 1) {
		return dp[l][r] = 2;
	}

	let res = 0;
	
    //パターン1.
    if (Math.abs(w[l] - w[r]) <= 1 && dp[l + 1] != null && kukanDP(w, l + 1, r - 1) === Math.abs(r - l) - 1){
		// console.log("here", l, r);
        //[l , r)がはじき出せるので
        res = Math.abs(r - l) + 1;
    }

    //パターン２.区間を分ける
    for(let mid = l; mid <= r - 1; mid++){
        res = Math.max(res , kukanDP(w, l, mid) + kukanDP(w, mid + 1, r));
	}
	// console.log("2",l,r, dp);
    return dp[l][r] = res;
};

Main(require("fs").readFileSync("/dev/stdin", "utf8"));


