// 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>

#include <numeric>

#include <functional>

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 int 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;}



typedef pair<int, int> pii;

typedef long long ll;



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

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

constexpr ll INF = 1001001001001001LL;

constexpr ll MOD = 1000000007LL;



int N, w[310];

bool dp[310][310];

int rec[310][310];



signed main() {

    while(cin >> N, N) {

        memset(dp, false, sizeof(dp));

        memset(rec, 0, sizeof(rec));



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



        repq(x,2,N) rep(l,0,N-x+1) {

            int r = l+x;

            if(x == 2) {

                if(abs(w[l] - w[l+1]) <= 1) dp[l][r] = true;

            }

            else {

                rep(k,l,r) if(dp[l][k] && dp[k][r]) dp[l][r] = true;

            }



            if(dp[l][r]) {

                int c = 1;

                while(1) {

                    int nl = l - c, nr = r + c;

                    if(nl < 0 || nr > N) break;

                    if(abs(w[nl] - w[nr-1]) > 1) break;

                    dp[nl][nr] = true; c++;

                }

                rec[l][r] = r - l;

            }

        }



        repq(x,2,N) rep(l,0,N-x+1) {

            int r = l+x;

            rep(k,l,r) chmax(rec[l][r], rec[l][k] + rec[k][r]);

        }

        cout << rec[0][N] << endl;

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.1893189947230134x):
// #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;

}