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++: // vvvvvvvvvvvv TEMPLATE vvvvvvvvvvvv
#include <bits/stdc++.h>
using namespace std; using ll = long long; using P = pair<ll, ll>;
const ll linf = 1e18; const double eps = 1e-12, pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define each(i,a) for (auto&& i : a)
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)
template<typename T> ll min(ll a, const T&& b) { return a < b ? a : b; }
template<typename T> ll min(const T&& a, ll b) { return a < b ? a : b; }
template<typename T> ll max(ll a, const T&& b) { return a > b ? a : b; }
template<typename T> ll max(const T&& a, ll b) { return a > b ? a : b; }
template<typename Head> void out(Head h) { cout << h << endl; } template<typename Head, typename... Tail>void out(Head h, Tail... t) { cout << h << " "; out(t...); }
template<typename T> istream& operator>>(istream& is, vector<T>& v) { each(x,v) is >> x; return is; }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) { rep(i,v.size()) { if (i) os << " "; os << v[i]; } return os; }
ostream& operator<<(ostream& os, const vector<string>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) { rep(i,v.size()) { if (i) os << endl; os << v[i]; } return os; }
template<typename T1, typename T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; }
struct yes_no : std::numpunct<char> { string_type do_truename() const { return "Yes"; } string_type do_falsename() const { return "No"; } };
void solve(); int main() {
  ios::sync_with_stdio(false); cin.tie(0); locale loc(locale(), new yes_no); cout.imbue(loc); cout << fixed << setprecision(10) << boolalpha;
  solve();
}
// ^^^^^^^^^^^^ TEMPLATE ^^^^^^^^^^^^

void solve() {
  ll n;
  while (cin >> n, n) {
    vector<ll> a(n); cin >> a;
    vector<vector<bool>> dp(n+1, vector<bool>(n+1, false));
    rep(i, n+1) dp[i][i] = true;
    rep(w, 2, n+1) rep(l, n) {
      ll r = l + w;
      if (r > n) continue;
      if (dp[l+1][r-1] && abs(a[l] - a[r-1]) <= 1) {
        dp[l][r] = true;
      }
      rep(m, l+1, r) {
        if (dp[l][m] && dp[m][r]) {
          dp[l][r] = true;
        }
      }
    }
    vector<ll> dp2(n+1, 0);
    rep(i, n) {
      chmax(dp2[i+1], dp2[i]);
      rep(j, i, n+1) {
        if (dp[i][j]) {
          chmax(dp2[j], dp2[i] + j-i);
        }
      }
    }
    ll ans = dp2[n];
    cout << ans << endl;
  }
}

C++: #include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = (long long)1e9 + 7; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

#define MAX

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	while(true) {
		int n;
		vector<int> w;
		vector<vector<int>> dp;
		
		cin >> n;
		
		if(!n) break;
		
		w.resize(n);
		dp.resize(n+1, vector<int>(n+1, 0));
		
		for(int i = 0; i < n; i++){
			cin >> w[i];
		}
		
		
		for(int k = 2; k <= n; k++){
			for(int i = 0; i < n && i + k <= n; i++){
				int j = i + k;
				
				if(abs(w[i] - w[j-1]) <= 1 && dp[i+1][j-1] == k - 2){
					dp[i][j] = k;
				}
				
				for(int l = i + 1; l < j; l++){
					dp[i][j] = max(dp[i][j], dp[i][l] + dp[l][j]);
				}
			}
		}
		
		cout<<dp[0][n]<<endl;
	}
	
	
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> memo;
vector<int> sz;

int dp(int l, int r) {
  // dp[i,j] := [i,j]で出せるブロックの最大値
  if (memo[l][r] != -1)
    return memo[l][r];
  if (l == r)
    return memo[l][r] = 0;
  if (r - l == 1) {
    if (abs(sz[r] - sz[l]) <= 1)
      return memo[l][r] = 2;
    else
      return memo[l][r] = 0;
  }
  if (dp(l + 1, r - 1) == r - l - 1 && abs(sz[r] - sz[l]) <= 1) {
    return memo[l][r] = r - l + 1;
  } else {
    int res = 0;
    for (int i = l; i <= r - 1; i++) {
      res = max(res, dp(l, i) + dp(i + 1, r));
    }
    return memo[l][r] = res;
  }
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    sz.resize(n);
    for (int i = 0; i < n; i++) {
      cin >> sz[i];
    }
    memo.assign(n, vector<int>(n, -1));
    cout << dp(0, n - 1) << endl;
  }
}

C++: #include <vector>
#include <iostream>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int n;
int main() {
	while (scanf("%d", &n), n) {
		vector<int> a(n);
		for (int i = 0; i < n; i++) scanf("%d", &a[i]);
		vector<vector<int> > dp(n, vector<int>(n + 1));
		for (int i = 2; i <= n; i++) {
			for (int j = 0; j <= n - i; j++) {
				int r = j + i;
				for (int k = j + 1; k < r; k++) {
					dp[j][r] = max(dp[j][r], dp[j][k] + dp[k][r]);
				}
				if (abs(a[j] - a[r - 1]) <= 1 && dp[j + 1][r - 1] == i - 2) {
					dp[j][r] = max(dp[j][r], dp[j + 1][r - 1] + 2);
				}
			}
		}
		printf("%d\n", dp[0][n]);
	}
	return 0;
}