#include <iostream>
#include <cmath>
#define INF 1e+9
using namespace std;

int main(){
	int n;
	while(cin >> n,n){
		int a[300],dp2[301] = {};
		bool dp1[301][301] = {};
		for(int i = 0;i < n;i++){
			cin >> a[i];
			dp1[i][i] = true;
		}
		for(int i = 2;i <= n;i += 2){
			for(int j = 0;j + i <= n;j++){
				for(int k = 0;k <= i;k++){
					if(dp1[j][j + k] && dp1[j + k][j + i]) dp1[j][j + i] = true;
				}
				if(dp1[j + 1][j + i - 1] && abs(a[j] - a[j + i - 1]) <= 1) dp1[j][j + i] = true;
			}
		}
		for(int i = 1;i <= n;i++){
			dp2[i] = dp2[i - 1] + 1;
			for(int j = 0;j <= i;j++){
				if(dp1[j][i]) dp2[i] = min(dp2[i],dp2[j]);
			}
		}
		cout << n - dp2[n] << endl;
	}
	return 0;
}