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

int dp[305][305], w[305];

int rec(int l, int r){
    if(r-l==1) return 0;
    if(r-l==2){
        if(abs(w[r-1]-w[l])<=1) return 2;
        else return 0;
    }

    if(dp[l][r]>-1) return dp[l][r];
    if(abs(w[r-1]-w[l])<=1 && rec(l+1, r-1)==r-l-2) return dp[l][r] = r - l;

    int res = 0;
    for(int i=l;i<r-1;i++){
        res = max(res, rec(l, i+1)+rec(i+1, r));
    }
    return dp[l][r] = res;
}

int main(){
    ios::sync_with_stdio(false), cin.tie(nullptr);
    while(true){
        int N;cin>>N;
        if(!N)return 0;
        for(int i=0;i<N;i++)cin>>w[i];
        for(int i=0;i<=N;i++) for(int j=0;j<=N;j++) dp[i][j] = -1;
        printf("%d\n", rec(0, N));
    }
}
