#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
using namespace std;
typedef long long LL;
const int INF = 999999999;

int dp[334][334];
int w[334];

int rec(int l, int r){
    assert(l < r);
    if(dp[l][r] >= 0) return dp[l][r];
    if(r-l == 1) return dp[l][r] = 0;
    int rm = (abs(w[r-1]-w[l]) <= 1 ? 2 : 0);
    if(r-l == 2) return dp[l][r] = rm;
    int ret = rec(l+1, r-1);
    if(ret == r-l-2) ret += rm;
    for(int mid=l+1; mid<r; mid++) {
        ret = max(ret, rec(l, mid)+rec(mid, r));
    }
    return dp[l][r] = ret;
}

int main(){
    int n;
    cin >> n;
    while(n > 0){
        memset(dp, -1, sizeof(dp));
        rep(i, n) cin >> w[i];
        cout << rec(0, n) << endl;

        // rep(i, n+1){
        //     rep(j, n+1) cout << dp[i][j] << " ";
        //     cout << endl;
        // }
        cin >> n;
    }
}


