// Slower C++ solution:
#include <iostream>

#include <iomanip>

#include <cstdio>

#include <string>

#include <cstring>

#include <deque>

#include <list>

#include <queue>

#include <stack>

#include <vector>

#include <utility>

#include <algorithm>

#include <map>

#include <set>

#include <complex>

#include <cmath>

#include <limits>

#include <cfloat>

#include <climits>

#include <ctime>

#include <cassert>

using namespace std;



#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)

#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)

#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)

#define all(v) begin(v), end(v)

#define pb(a) push_back(a)

#define fr first

#define sc second

#define INF 2000000000

#define int long long int



#define X real()

#define Y imag()

#define EPS (1e-10)

#define EQ(a,b) (abs((a) - (b)) < EPS)

#define EQV(a,b) ( EQ((a).X, (b).X) && EQ((a).Y, (b).Y) )

#define LE(n, m) ((n) < (m) + EPS)

#define LEQ(n, m) ((n) <= (m) + EPS)

#define GE(n, m) ((n) + EPS > (m))

#define GEQ(n, m) ((n) + EPS >= (m))



typedef vector<int> VI;

typedef vector<VI> MAT;

typedef pair<int, int> pii;

typedef long long ll;



typedef complex<double> P;

typedef pair<P, P> L;

typedef pair<P, double> C;



int dy[]={0, 0, 1, -1};

int dx[]={1, -1, 0, 0};

int const MOD = 1000000007;



namespace std {

    bool operator<(const P& a, const P& b) {

        return a.X != b.X ? a.X < b.X : a.Y < b.Y;

    }

}



bool take[310][310];



signed main() {

    int n;

    while(cin >> n, n) {

        int w[310];

        rep(i,0,n) cin >> w[i];

        memset(take, false, sizeof(take));



        rep(i,0,n) {

            rep(j,0,n-i) {

                int len = i+1;

                int s = j, t = j+i;

                rep(k,s,t) {

                    if(take[s][k] && take[k+1][t]) take[s][t] = true;

                }

                if(len == 2) {

                    if(abs(w[s] - w[t]) < 2) {

                        take[s][t] = true;

                    }

                }

                if(take[s][t]) {

                    int x = s-1, y = t+1;

                    while(1) {

                        if(x < 0 || x >= n || y < 0 || y >= n) break;

                        if(abs(w[x] - w[y]) >= 2) break;

                        take[x][y] = true;

                        x--; y++;

                    }

                }

            }

        }



        int dp[310][310] = {};

        rep(i,0,n) rep(j,i+1,n) {

            if(take[i][j]) dp[i][j] = j-i+1;

        }



        rep(i,0,n) rep(j,i,n) rep(k,i,j) {

            dp[i][j] = max(dp[i][j], dp[i][k] + dp[k+1][j]);

        }

        cout << dp[0][n-1] << endl;

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.1999415334345667x):
// #define _GLIBCXX_DEBUG // for STL debug (optional)

#include <iostream>

#include <iomanip>

#include <cstdio>

#include <string>

#include <cstring>

#include <deque>

#include <list>

#include <queue>

#include <stack>

#include <vector>

#include <utility>

#include <algorithm>

#include <map>

#include <set>

#include <complex>

#include <cmath>

#include <limits>

#include <cfloat>

#include <climits>

#include <ctime>

#include <cassert>

#include <numeric>

#include <fstream>

#include <functional>

#include <bitset>

using namespace std;

using ll = long long int;

using int64 = long long int;

 

template<typename T> void chmax(T &a, T b) {a = max(a, b);}

template<typename T> void chmin(T &a, T b) {a = min(a, b);}

template<typename T> void chadd(T &a, T b) {a = a + b;}

 

int dx[] = {0, 0, 1, -1};

int dy[] = {1, -1, 0, 0};

const int INF = 1LL << 29;

const ll LONGINF = 1LL << 60;

const ll MOD = 1000000007LL;



int dp1[310][310], dp2[310];

int solve_testcase() {

    int N; scanf("%d", &N);

    if(N == 0) return 1;

    vector<int> A(N);

    for(int i=0; i<N; i++) scanf("%d", &A[i]);

    

    fill(dp1[0], dp1[N+1], false);

    for(int i=0; i<N; i++) {

        dp1[i][i] = true;

    }

    for(int d=2; d<=N; d++) {

        for(int l=0; l<N-d+1; l++) {

            int r = l + d;

            if(abs(A[l] - A[r-1]) <= 1 and dp1[l+1][r-1]) dp1[l][r] = true;



            if(r - l >= 4) {

                for(int k=l+2; k<=r-2; k++) {

                    if(dp1[l][k] and dp1[k][r]) dp1[l][r] = true;

                }

            }

        }

    }



    fill(dp2, dp2 + N + 1, -1);

    dp2[0] = 0;

    for(int i=0; i<N; i++) {

        chmax(dp2[i+1], dp2[i]);

        for(int j=i+1; j<=N; j++) {

            if(!dp1[i][j]) continue;

            chmax(dp2[j], dp2[i] + (j - i));

        }

    }

    printf("%d\n", dp2[N]);

    return 0;

}



int main() {

    while(!solve_testcase());

    return 0;

}