#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>

using namespace std;
typedef long long ll;

int N;
int dp[301][301];
int A[300];

int main() {
	while (1) {
		cin >> N;
		if (N == 0)return 0;
		
		for (int i = 0; i <= 300; i++)for (int j = 0; j <= 300; j++)dp[i][j] = 0;

		for (int i = 0; i < N; i++)cin >> A[i];

		for (int W = 2; W <= N; W++) {
			for (int i = 0;; i++) {
				int j = i + W - 1;
				if (j >= N)break;
				if (dp[i + 1][j - 1] == W - 2 && abs(A[i] - A[j]) <= 1)dp[i][j] = W;
				for (int k = i; k < j; k++) {
					dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j]);
				}
			}
		}
		cout << dp[0][N - 1] << endl;

	}
	return 0;
}
