#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int main() {
	while (cin >> n, n) {
		vector<int> a(n);
		for (int i = 0; i < n; i++) cin >> a[i];
		vector<vector<int> > dp(n, vector<int>(n + 1));
		for (int i = 2; i <= n; i++) {
			for (int j = 0; j <= n - i; j++) {
				int r = j + i;
				for (int k = j + 1; k < r; k++) {
					dp[j][r] = max(dp[j][r], dp[j][k] + dp[k][r]);
				}
				if (abs(a[j] - a[r - 1]) <= 1 && dp[j + 1][r - 1] == i - 2) {
					dp[j][r] = max(dp[j][r], dp[j + 1][r - 1] + 2);
				}
			}
		}
		cout << dp[0][n] << endl;
	}
	return 0;
}