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

int w[300];
int dp[310][310];

int main(){
    int n;
    while(cin >> n, n){
        for(int i = 0; i < n; i++) cin >> w[i];
        memset(dp, 0, sizeof dp);

        for(int len = 2; len <= n; len++){
            for(int l = 0; l + len <= n; l++){
                for(int c = l + 1; c < l + len; c++){
                    dp[l][l + len] = max(dp[l][l + len], dp[l][c] + dp[c][l + len]);
                }
                if(abs(w[l] - w[l + len - 1]) <= 1 && dp[l + 1][l + len - 1] == len - 2){
                    dp[l][l + len] = len;
                }
                //printf("[%d, %d) = %d\n", l, l + len, dp[l][len] );
            }
        }

        cout << dp[0][n] << endl;
    }

}