#include <bits/stdc++.h>
using namespace std;

int n;
int w[310], dp[310][310];

int rec(int i, int j) {
    if (dp[i][j] >= 0) return dp[i][j];
    if (i >= j) return dp[i][j] = 0;
    int res = 0;
    for (int k = i + 1; k < j; ++k) {
        res = max(res, rec(i, k) + rec(k, j));
    }
    if ((j - i) % 2 == 0 && abs(w[i] - w[j - 1]) <= 1) {
        if (rec(i + 1, j - 1) == j - i - 2) {
            res = max(res, dp[i + 1][j - 1] + 2);
        }
    }
    return dp[i][j] = res;
}

int main() {
    while (cin >> n, n) {
        for (int i = 0; i < n; ++i) {
            cin >> w[i];
        }
        memset(dp, -1, sizeof(dp));
        cout << rec(0, n) << endl;
    }
}
