// Slower C++ solution:
#include <stdio.h>

#include <cmath>

#include <algorithm>

#include <stack>

#include <queue>

#include <vector>

typedef long long int ll;

#define BIG_NUM 2000000000

#define MOD 1000000007

using namespace std;



bool** table;

int** dp;

int N;



int recursive(int left,int right){



	if(left == right)return dp[left][right] = 0;

	if(dp[left][right] != -1)return dp[left][right]; //?????¢???????????´???

	if((right-left+1)%2 == 1){ //??????????\???°?????´???

		return dp[left][right] = max(recursive(left,right-1),recursive(left+1,right));

	}

	if(table[left][(right-left+1)/2] == true)return dp[left][right] = right-left+1; //left???right?????¨????¶?????????´???

	if(right == left+1 && table[left][1] == false)return 0;



	int ans = 0;

	for(int i = left+1; i <= right-2; i++){

		ans = max(ans,recursive(left,i)+recursive(i+1,right));

	}

	return dp[left][right] = ans;

}



int main(){



	int data[301];

	table = new bool*[301]; //table[left][right] = left???????§?????????????2*right??????????????¨?¶??????§??????????????????

	dp = new int*[301]; //?????????????????????????????§?¶????????????°

	for(int i = 0; i <= 300; i++){

		table[i] = new bool[151];

		dp[i] = new int[301];

	}



	bool FLG;



	while(true){

		scanf("%d",&N);

		if(N == 0)break;



		if(N <= 1){

			printf("0\n");

			continue;

		}



		//?¶??????????????????????????????????

		for(int i = 0; i <= 300; i++){

			for(int k = 0; k <= 150; k++)table[i][k] = false;

			for(int k = 0; k <= 300; k++){

				dp[i][k] = -1;

			}

		}



		for(int i = 1; i <= N; i++)scanf("%d",&data[i]);



		//????????????2???????¨?

		for(int i = 1; i <= N-1; i++){

			if(abs(data[i+1]-data[i]) <= 1)table[i][1] = true;



		}



		//?¶??????????????????????????¨????

		for(int length = 4; length <= N; length += 2){ //??????????????????

			for(int left = 1; left+length -1 <= N; left++){ //??????????????????

				FLG = false;



				if(table[left+1][(length-2)/2] == true && abs(data[left] - data[left+length-1]) <= 1)FLG = true;//??????????????£?????????length-2??????????¶????????????????????????¨??????+length-1?????´???????????????????¶?????????´???



				if(!FLG){

					for(int i = 2; i <= length-2; i += 2){

						if(table[left][i/2] == true && table[left+i][(length-i)/2] == true){

							FLG = true;

							break;

						}

					}

				}

				if(FLG){

					table[left][length/2] = true;

				}

			}

		}



		printf("%d\n",recursive(1,N));

	}



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.5573721183709894x):
#include <stdio.h>

#include <cmath>

#include <algorithm>

#include <stack>

#include <queue>

#include <vector>

typedef long long int ll;

#define BIG_NUM 2000000000

#define MOD 1000000007

using namespace std;



bool** table;

int** dp;

int N;



int recursive(int left,int right){



	if(dp[left][right] != -1)return dp[left][right]; //?????¢???????????´???

	if((right-left+1)%2 == 1){ //??????????\???°?????´???

		return dp[left][right] = max(recursive(left,right-1),recursive(left+1,right));

	}

	//if(table[left][(right-left+1)/2] == true)return dp[left][right] = right-left+1; //left???right?????¨????¶?????????´???

	//if(right == left+1 && table[left][1] == false)return dp[left][right] = 0;



	for(int i = left+1; i <= right-2; i++){

		dp[left][right] = max(dp[left][right],recursive(left,i)+recursive(i+1,right));

	}

	return dp[left][right];

}



int main(){



	int data[301];

	table = new bool*[301]; //table[left][right] = left???????§?????????????2*right??????????????¨?¶??????§??????????????????

	dp = new int*[301]; //?????????????????????????????§?¶????????????°

	for(int i = 0; i <= 300; i++){

		table[i] = new bool[151];

		dp[i] = new int[301];

	}



	bool FLG;



	while(true){

		scanf("%d",&N);

		if(N == 0)break;



		if(N <= 1){

			printf("0\n");

			continue;

		}



		//?¶??????????????????????????????????

		for(int i = 0; i <= N; i++){

			for(int k = 0; k <= N/2; k++)table[i][k] = false;

			for(int k = 0; k <= N; k++){

				if(i == k)dp[i][k] = 0;

				else{

					dp[i][k] = -1;

				}

			}

		}



		for(int i = 1; i <= N; i++)scanf("%d",&data[i]);



		//????????????2???????¨?

		for(int i = 1; i <= N-1; i++){

			if(abs(data[i+1]-data[i]) <= 1){

				table[i][1] = true;

				dp[i][i+1] = 2;

			}else{

				dp[i][i+1] = 0;

			}

		}



		//?¶??????????????????????????¨????

		for(int length = 4; length <= N; length += 2){ //??????????????????

			for(int left = 1; left+length -1 <= N; left++){ //??????????????????

				FLG = false;



				if(table[left+1][(length-2)/2] == true && abs(data[left] - data[left+length-1]) <= 1)FLG = true;//??????????????£?????????length-2??????????¶????????????????????????¨??????+length-1?????´???????????????????¶?????????´???



				if(!FLG){

					for(int i = 2; i <= length-2; i += 2){

						if(table[left][i/2] == true && table[left+i][(length-i)/2] == true){

							FLG = true;

							break;

						}

					}

				}

				if(FLG){

					table[left][length/2] = true;

					dp[left][left+length-1] = length;

				}

			}

		}



		printf("%d\n",recursive(1,N));

	}



	return 0;

}