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++: #define _GLIBCXX_DEBUG

#include <bits/stdc++.h>


using namespace std;

#define DUMP(x) cout << #x << ": " << x << endl

int n;
int memo[310][310] = { false };
int dp[310] = { 0 };

vector<int> w(n);

int rec(int l, int r) {
  if (r - l == 0) {
    return memo[l][r] = 2;
  }
  if (r - l == 1) {
    return memo[l][r] = 1;
  }
  
  if (r - l  - 1 == 1) {
    if (abs(w[r - 1] - w[l]) <= 1) {
      return memo[l][r] = 2;
    }
  }
  if (memo[l][r]) {
    return memo[l][r];
  }

  for (int t = l + 1; t < r; t++) {
    int temp  = rec(l, t);
    int temp2 = rec(t, r);
    int temp3 = rec(l + 1, t);
    int temp4 = rec(t, r - 1);
    
    if (temp == 2 && temp2 == 2) {
      return memo[l][r] = 2;
    }
    if (abs(w[l] - w[r - 1]) <= 1 && temp3 == 2 && temp4 == 2) {
      return memo[l][r] = 2;
    }
  }

  return memo[l][r] = 1;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }

    w.clear();
    w.resize(n);

    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }
    memset(memo, 0, sizeof(memo));
    memset(dp, 0, sizeof(dp));
    
    //    cerr << " " << rec(0, n) << endl;
    rec(0, n);

    
    dp[0] = 0;

    for (int r = 0; r <= n; r++) {
      dp[r] = dp[r - 1];
      for (int l = 0; l < r; l++) {
        if (memo[l][r] == 2) {
          dp[r] = max(dp[r], dp[l] + (r - l));
        }
      }
    }
    

    cout << dp[n] << endl;

  }

  

  return 0;
}

C++: #include <iostream>
#include <bitset>
#include <vector>

using namespace std;

const int N = 10000000;

vector<vector<int>> mem;
vector<int> bs;

int rec(int l, int r) {
  if(mem[l][r] >= 0) return mem[l][r];
  if(l >= r) {
    mem[l][r] = 0;
  } else {
    int d = r - l + 1;
    if(rec(l+1, r-1) == d - 2 && abs(bs[l] - bs[r]) <= 1) {
      mem[l][r] = d;
    } else {
      int ans = 0;
      for(int k = l; k < r; k++) {
        ans = max(ans, rec(l, k) + rec(k+1, r));
      }
      mem[l][r] = ans;
    }
  }
  return mem[l][r];
}

int main(int argc, char *argv[])
{
  for(;;) {
    int m;
    cin >> m;
    if(m == 0) break;
    bs.clear();
    for(int i = 0; i < m; i++) {
      int b;
      cin >> b;
      bs.push_back(b);
    }
    mem = vector<vector<int>>(m, vector<int>(m, -1));
    int ans = rec(0, m-1);
    cout << ans << endl;
  }
  return 0;
}


/*

  f(i, j) : bs[i..j] ??????????????§?????°

 f(i,j) =
    0          ... i >= j ?????¨??????????????????
    j - i + 1  ... if bs[i] ??¨ bs[j] ????????? 1 ??\?????§???i+1??????j-1???????????????????????????????????????????????????????????????????????????
    f(i, k) + f(k + 1, j) ... ???????????§?????£????????????

*/

C++: #include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<(int)(n);i++)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; }
template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; }
template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
//constexpr double eps = 1e-14; 
constexpr double eps = 1e-10; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

void solve(int N){
    vector<int> w(N);
    rep(i,N) cin >> w[i];

    vvi dp(N,vi(N,0));
    for(int len=0;len<=N;len++){
        for(int i=0;i<N;i++){
            int j=i+len-1;
            if(j>=N) continue;
            if(i+1<N and j-1>=0){
                int d=0;
                if(dp[i+1][j-1]==(j-1)-(i+1)+1 and abs(w[i]-w[j])<=1) d=2;
                dp[i][j]=max(dp[i][j],dp[i+1][j-1]+d);
            }
            if(j-2>=max(i,0)){
                int d=0;
                if(abs(w[j]-w[j-1])<=1) d=2;
                dp[i][j]=max(dp[i][j],dp[i][j-2]+d);
            }
            if(i+2<=j){
                int d=0;
                if(abs(w[i]-w[i+1])<=1) d=2;
                dp[i][j]=max(dp[i][j],dp[i+2][j]+d);
            }
        }
    }
    cout << dp[0][N-1] << endl;
}

int main(){
    while(1){
        int n;
        cin >> n;
        if(n==0) break;
        solve(n);
    }
}