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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int n, a[1010];
int dp[1010][1010], dp2[1010] = {};

int ok(int l, int r){
    int &res = dp[l][r];
    if(res != -1) return res;
    res = false;
    if(l == r) return res = true;
    for(int i = l+2; i < r; i += 2) if(ok(l,i) && ok(i,r)) return res = true;
    if(abs(a[l] - a[r-1]) <= 1 && ok(l+1, r-1)) return res = true;
    return res;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    while(cin >> n && n){
        rep(i, n) cin >> a[i];
        memset(dp, -1, sizeof dp);
        memset(dp2, 0, sizeof dp2);
        rep(i, n){
            dp2[i+1] = max(dp2[i+1], dp2[i]);
            for(int j = i + 2; j <= n; j += 2){
                if(ok(i, j)) dp2[j] = max(dp2[j], dp2[i] + j - i);
            }
        }
        cout << dp2[n] << '\n';
    }
}

C++: //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1611&lang=jp
//ダルマ落とし
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll=long long;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
template<class T>bool chmax(T &a, const T &b) {if (a<b) { a=b; return 1; } return 0;}
template<class T>bool chmin(T &a, const T &b) {if (b<a) { a=b; return 1; } return 0;}
#if 0
ll solve(){
  ll n; cin>>n;
  if(n==0) return -1;
  vector<ll> wn(n);
  for(auto&& w:wn) cin>>w;

  auto dfs = [&](auto&& self,ll l, ll r)->ll{
    ll ret=0;
    if(l+1>=r) return ret;
    FOR(k,l,r-1){
      if(abs(wn.at(k)-wn.at(k+1))>=2)continue;
      chmax(ret, self(self,l,k)+self(self,k+2,r)+2);
    }
    return ret;
  };

  ll ans=dfs(dfs, 0,n);
  cout<<ans<<endl;
  return ans;
}
#endif
ll solve2(){
  ll n; cin>>n;
  if(n==0) return -1;
  vector<ll> wn(n);
  for(auto&& w:wn) cin>>w;

  vector<vector<ll>> dp(n,vector<ll>(n+1));
  FOR(len,1,n){
    FOR(l,0,n){
      ll r=l+len;
      if (r>=n)break;
      if(len-1 == dp[l+1][r-1]){
        if (abs(wn.at(l)-wn.at(r))<2){
          dp[l][r] = dp[l+1][r-1]+2;
        } 
      }
      dump("-",len,l,r);
      //kで区切る
      for(ll k=l;k<r;++k){
        chmax(dp[l][r],dp[l][k]+dp[k+1][r]);
        dump(l,k,k+1,r,dp[l][r]);
      }
    }
  }
  dump(dp);
  ll ans=dp[0][n-1];
  cout<<ans<<endl;
  return ans;
}
int main() {
#if LOCAL&01
  //std::ifstream in("./test/sample-1.in");
  std::ifstream in("./input.txt");
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  while(solve2() >= 0){
    //break;
  }
  return 0;
}

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

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

ll n;
vector<ll> a;
vector<vector<ll>> table;

#define DP table[left][right]
ll dfs(ll left, ll right) {
  if (DP != -1) return DP;

  if (right - left <= 1) return DP = 0;
  ll maxv = 0;
  if (abs(a[left] - a[right - 1]) < 2) {
    ll value = dfs(left + 1, right - 1) + 2;
    if (value == right - left) maxv = value;
  }
  FOR(i, left + 1, right - 1) {
    maxv = max(maxv, dfs(left, i) + dfs(i, right));
  }

  return DP = maxv;
}

bool solve() {
  cin >> n;
  if (n == 0) return false;
  a = vector<ll>(n);
  REP(i, n) cin >> a[i];
  table = vector<vector<ll>>(n + 1, vector<ll>(n + 1, -1));

  cout << dfs(0, n) << endl;
  return true;
}

int main() {
  while(solve());
}