#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;

int main() {
    int n;
    while (cin >> n, n) {
        vi a(n);
        for (int i=0; i<n; ++i) cin >> a[i];

        vvi dp(301, vi(301, 0));
        for (int i=0; i+2<=n; ++i) {
            dp[i][i+2] = (abs(a[i] - a[i+1]) <= 1 ? 2 : 0);
        }
        for (int l=3; l<=n; ++l) {
            for (int i=0; i+l<=n; ++i) {
                if (dp[i+1][i+l-1] == l-2) {
                    dp[i][i+l] = dp[i+1][i+l-1] + (abs(a[i] - a[i+l-1]) <= 1 ? 2 : 0);
                }
                for (int j=i+1; j<i+l; ++j) {
                    dp[i][i+l] = max(dp[i][i+l], dp[i][j] + dp[j][i+l]);
                }
            }
        }
        cout << dp[0][n] << endl;
    }
    return 0;
}
