#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

int n;
int w[333];
int dp[333][333];

int rec(int le, int ri) {
    if (~dp[le][ri]) return dp[le][ri];
    if (le == ri) {
        return dp[le][ri] = 0;
    }
    if (ri - le + 1 == 2) {
        return dp[le][ri] = (abs(w[le] - w[ri]) <= 1 ? 2 : 0);
    }
    if (abs(w[le] - w[ri]) <= 1) {
        if (ri - 1 >= le + 1) {
            int t = (ri - 1) - (le + 1) + 1;
            dp[le][ri] = (rec(le + 1, ri - 1) == t ? rec(le + 1, ri - 1) + 2 : dp[le][ri]);
        }
    }
    for (int md = le + 1; md <= ri; md++) {
        dp[le][ri] = max(dp[le][ri], rec(le, md - 1) + rec(md, ri));
    }
    return dp[le][ri];
}

int main() {
    while (cin >> n && n) {
        for (int i = 0; i < n; i++) cin >> w[i];
        for (int i = 0; i < 333; i++) for (int j = 0; j < 333; j++) {
            dp[i][j] = -1;
        }
        int ans = max(0, rec(0, n - 1));
        cout << ans << endl;
    }
}
