Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 10000000
	maxBufSize     = 100000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	a, _ := strconv.Atoi(sc.Text())
	return int(a)
}
func nextString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextIntSlice(sc *bufio.Scanner, n int) (a []int) {
	a = make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = nextInt(sc)
	}
	return a
}

var dp [][]int

func initDP(n int) {
	dp = make([][]int, n+1)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n+1)
		for j := 0; j < len(dp[0]); j++ {
			dp[i][j] = -1
		}
	}
}

func resolve() {
	ans := []int{}
	for true {
		n := nextInt(sc)
		if n == 0 {
			break
		}
		as := nextIntSlice(sc, n)
		initDP(n)
		_rec(as, n, 0, n)
		ans = append(ans, dp[0][n])
	}
	for _, v := range ans {
		fmt.Println(v)
	}
	return
}

func _rec(as []int, n, l, r int) int {
	if dp[l][r] > -1 {
		return dp[l][r]
	}
	if r-l == 1 { // prevent to hit daruma
		dp[l][r] = 0
		return dp[l][r]
	}
	if r-l == 2 {
		if abs(as[l]-as[r-1]) <= 1 {
			dp[l][r] = 2
		} else {
			dp[l][r] = 0
		}
		return dp[l][r]
	}
	if abs(as[l]-as[r-1]) <= 1 {
		v := _rec(as, n, l+1, r-1)
		// if we can remove internal blocks
		if (r - 1 - (l + 1)) == v {
			dp[l][r] = v + 2
			return dp[l][r]
		} else {
			dp[l][r] = v
		}
	}
	for i := l + 1; i < r; i++ {
		v := _rec(as, n, l, i) + _rec(as, n, i, r)
		if dp[l][r] < v {
			dp[l][r] = v
		}
	}
	return dp[l][r]
}

func abs(v int) int {
	if v < 0 {
		return -v
	}
	return v
}

C++: #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;
}

C++: #include "bits/stdc++.h"
using namespace std;

//#define rep(i, n)     for(auto i = 0; i != n; ++i)
#define REP(i, k, n)  for(auto i = k; i != n; ++i)
//#define rrep(i, n)    for(auto i = n; i != 0; --i)
#define RREP(i, n, k) for(auto i = n; i != k; --i)

#define debug(x) cerr << #x << " " << x << endl
#define spc << " " <<

#define mp make_pair
#define mt make_tuple

using ll = long long;
using Pii = pair<int, int>;
using Tiii = tuple<int, int, int>;
template<typename T>using V = vector<T>;
using Vi = vector<int>;
using VVi = vector<vector<int>>;



int main() {
   cin.tie(nullptr);
   ios::sync_with_stdio(false);

   int n;
   while (cin >> n, n) {
       Vi w(n+1);
       REP(i, 1, n+1) cin >> w[i];

       if (n < 2) {
           cout << 0 << endl;
           continue;
       }

       VVi dp(n+1, Vi(n+1, -1));

       REP(R, 2, n+1) {
           int L = R-1;
           int diff = w[R] - w[L];
           if (-1 <= diff && diff <= 1) dp[L][R] = 2;
           else dp[L][R] = 0;
       }

       REP(R, 3, n+1) {
           int L = R-2;
           int left_v = dp[L][R-1], bottom_v = dp[L+1][R];
           if (left_v != 0 || bottom_v != 0) dp[L][R] = 2;
           else dp[L][R] = 0;
       }

       REP(slc, 3, n) {
           REP(R, slc + 1, n + 1) {
               int L = R - slc;
               int left_v = dp[L][R-1], bottom_v = dp[L+1][R];
               dp[L][R] = max(left_v, bottom_v);

               if ((R - L) % 2 == 1) {
                   int diff = w[R] - w[L];
                   if (-1 <= diff && diff <= 1 && dp[L + 1][R - 1] == R - L - 1) {
                       dp[L][R] = max(dp[L][R], dp[L + 1][R - 1] + 2);
                   }
               }

               REP(mid, L+1, R-1)
                   dp[L][R] = max(dp[L][R], dp[L][mid] + dp[mid+1][R]);
           }
       }

//       REP(L, 1, n+1) {
//           REP(R, 1, n+1) {
//               cout << dp[L][R] << " ";
//           } cout << endl;
//       }

//       REP(diff, 2, n) {
//           REP(R, diff+1, n+1) {
//               int L = R - diff;
//               int left_v = dp[L][R-1], bottom_v = dp[L+1][R];
//               int left2_v = dp[L][R-2], bottom2_v = dp[L+2][R];
//               if (left_v == bottom_v && left_v != 0) {
//                   int n_val = left_v + 2;
//                   if (
//                           R >= n_val
//                           &&
//                           left2_v != 0
//                           &&
//                           bottom2_v != 0
//                   ) dp[L][R] = n_val;
//                   else dp[L][R] = left_v;
//               } else {
//                   dp[L][R] = max(left_v, bottom_v);
//               }
//           }
//       }

//       REP(L, 1, n+1) {
//           REP(R, 1, n+1) {
//               cout << dp[L][R] << " ";
//           } cout << endl;
//       }

       cout << dp[1][n] << endl;
   }

   return 0;
}