#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <complex> // complex<double> a(1.2 , 2.3);// real(): 1.2, imag()2.3
using namespace std;

#define MOD 1000000007
#define ll long long
#define ld long double
#define FOR(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rmsame(a) sort(all(a)),a.erase(unique(all(a)), a.end())
#define rmvector(a,b) rep(i,a.size())rep(j,b.size())if(a[i]==b[j]){a.erase(a.begin()+i);i--;break;}
template<typename X> bool exist(vector<X> vec, X item){return find(all(vec), item)!=vec.end();}

ll gcd(ll a, ll b){if(b==0) return a; return gcd(b, a%b);}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}

bool take[310][310];
ll dp[310][310]; // 数え上げる部分


ll solver(){
    ll N;
    vector<ll> daruma;
    cin >> N;
    
    rep(i,N){
        ll inp;
        cin >> inp;
        daruma.pb(inp);
    }
    
    // takeの初期化
    for(ll i = 0; i < N; i++){
        for(ll j = i+1; j< N; j++){
            take[i][j] = false;
            if((j == i+1) && abs(daruma[j]- daruma[i]) <= 1){
                take[i][j] = true;
            }
        }
    }
    
    // takeの更新
    for(ll i = 0 ; i < N ; i++){ // 長さ
        for(ll j = 0; j < N; j++){// start
            ll len = i+1;
            ll s = j;
            ll t = j+i;
            
            if(t >= N) break;
            
            // 左右の二つを足した結果。
            for(ll k = s+1; k < t; k++){
                if(take[s][k] && take[k+1][t]) {
                    take[s][t] = true;
                    break;
                }
            }
            
            // 前後伸びるやつ
            if((s-1 < 0) || (t+1 >= N)) continue;
            if(take[s][t]  && (abs(daruma[s-1] - daruma[t+1]) <= 1)) take[s-1][t+1] = true;
        }
    }
    
    return N;
}



void answer(ll N){
    if(N == 0) return;
    
    // dp初期化
    for(ll i = 0 ; i < N; i++){
        for(ll j = i+1; j < N; j++){
            if(take[i][j]) dp[i][j] = j-i+1;
            else dp[i][j] = 0 ;
        }
    }

    for(ll i = 0 ; i < N; i++){
        for(ll j = i+1; j < N; j++){
            for(ll k = i; k < j ; k++){
                dp[i][j] = max(dp[i][j], dp[i][k] + dp[k+1][j]);
            }
        }
    }

    cout << dp[0][N-1] << endl;
    return ;
}


void showtake(ll N){
    cout << "------------take------------" << endl;
    rep(i, N){
        rep(j, N){
            if(take[i][j]) cout << 1 << " ";
            else cout << 0 << " ";
        }
        cout << endl;
    }
    cout << "----------------------------" << endl;
}

void showanswer(ll N){
    cout << "------------answer----------" << endl;
    rep(i, N){
        rep(j, N){
            cout << dp[i][j] << " ";
        }
        cout << endl;
    }
    cout << "----------------------------" << endl;
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll N;
    while(1){
        N = solver();
        //showtake(N);
        answer(N);
        //showanswer(N);
        if(N == 0) return 0;
    }
    
    
    //cout << fixed << setprecision(16) << ans << endl;
    return 0;
}



Measured memory_bytes: 3960