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

int N;
vector<int> w;
int solve(){
    vector<vector<int>> dp(N+1, vector<int>(N+1, -1));
    for (int i = 0; i <= N; i++)
    {
        dp[i][i] = 0;
        if(i < N) dp[i][i+1] = 0;
    }

    for (int bet = 2; bet <= N; bet++)
    {
        for (int l = 0; l + bet <= N; l++)
        {
            int r = l + bet;
            if(dp[l+1][r-1] == bet - 2){
                if(abs(w[l] - w[r-1]) <= 1) dp[l][r] = bet;
                else dp[l][r] = bet - 2;
            }
            for (int mid = l+1; mid < r-1; mid++)
            {
                dp[l][r] = max(dp[l][r], dp[l][mid] + dp[mid][r]);
            }
        }
    }
    return dp[0][N];
}

int main() {
    while (cin >> N, N)
    {
        w.resize(N);
        for (int i = 0; i < N; i++) cin >> w[i];
        cout << solve() << endl;
    }
}
