// 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;

	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];

	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]);



		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;



				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.5590871183736699x):
#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;



 int main(){



	int N,data[301];

	bool** table = new bool*[301];

	int** 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++){

				dp[i][k] = 0;

			}

		}



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



		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 = 3; length <= N; length += 1){

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



				if(length%2 == 0){ //??????????\???°?????´???

					FLG = false;



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



					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;

					}else{

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

							dp[left][left+length-1] = max(dp[left][left+length-1],dp[left][i]+dp[i+1][left+length-1]);

						}

					}

				}else{

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

						dp[left][left+length-1] = max(dp[left][left+length-1],dp[left][i]+dp[i+1][left+length-1]);

					}

				}

			}

		}



		if(N%2 == 0)printf("%d\n",dp[1][N]);

		else{

			printf("%d\n",max(dp[1][N-1],dp[2][N]));

		}

	}



	return 0;

}