#include "bits/stdc++.h"
#define REP(i,n,N) for(int i=(n); i<(N); i++)
#define RREP(i,n,N) for(ll i=(N-1); i>=n; i--)
#define CK(n,a,b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define p(s) cout<<(s)<<endl
#define p2(a,b) cout<<(a)<<" "<<(b)<<endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int inf=1e9;

int dp[310][310];
int n;
int w[310];
int main() {
    while(cin>>n, n){
        REP(i,0,n) REP(j,0,n) dp[i][j] = 0;
        REP(i,0,n) cin>>w[i];
        for(int k=1;k<n;k++){
            REP(L,0,n-k){
                int R = L + k;
                if(dp[L+1][R-1] == k-1) dp[L][R] = max(dp[L][R], dp[L+1][R-1] + (abs(w[L]-w[R]) <= 1)*2);
                REP(i,L,R) dp[L][R] = max(dp[L][R], dp[L][i] + dp[i+1][R]);
            }
        }
        p(dp[0][n-1]);
    }


    return 0;
}
