#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;


const long long int mod = 1000000007;
const long long int INF = 19999999999999;

long long int N,w[305],dp[305][305]={};
int main()
{
    cout << fixed << setprecision(18);
    
    cin>>N;
    while(N!=0){
        for(long long int i = 1; i <= N; i++){
            cin>>w[i];
        }
        for(long long int i = 0; i <= N+1; i++){
            for(long long int j = 0; j <= N+1; j++){
                dp[i][j]=0;
            }
        }
        for(long long int i = 2; i <= N; i+=2){
            if(i==2){
                for(long long int j = 1; j < N; j++){
                    if(abs(w[j]-w[j+1])<=1){
                        dp[j][j+2]=2;
                    }else{
                        dp[j][j+2]=0;
                    }
                }
            }else{
                for(long long int j = 1; j < N-i+2; j++){
                    if(dp[j+1][j+i-1]==i-2){
                        if(abs(w[j]-w[j+i-1])<=1){
                            dp[j][j+i]=max(dp[j][j+i],i);
                        }else{
                            
                            dp[j][j+i]=max(dp[j][j+i],i-2);
                        }
                    }
                    for(long long int k = 0; k < i; k+=2){
                        dp[j][j+i]=max(dp[j][j+i],dp[j][j+k]+dp[j+k][j+i]);
                    }
                    
                }
            }
        }
        long long int res=0;
        if(N%2==1){
            res=max(dp[1][N],dp[2][N+1]);
        }else{
            res=dp[1][N+1];
        }
        cout<<res<<endl;
        cin>>N;
    }
}




Measured memory_bytes: 3828