// Slower C++ solution:
#include <bits/stdc++.h>

  

using namespace std;

  

#define rep(i,n) REP(i,0,n)

#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)

#define pb push_back

#define all(r) r.begin(),r.end()

#define rall(r) r.rbegin(),r.rend()

#define fi first

#define se second

  

typedef long long ll;

typedef vector<int> vi;

typedef vector<ll> vl;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

 

const int INF = 1e9;

const ll MOD = 1e9 + 7;

double EPS = 1e-8;



bool used[310][310];

bool dp[310][310];

vi w;



int dp2[310][310];

template<typename T> T chmax(T& a, const T& b){return a = max(a, b);}

template<typename T> T chmin(T& a, const T& b){return a = min(a, b);}

//[l, r) de tatakeruka

bool calc(int l, int r) {

	if(r - l <= 1) return false;

	if(used[l][r]) return dp[l][r];

	used[l][r] = true;

	if(r - l == 2) {

		return dp[l][r] = (abs(w[l] - w[r-1]) <= 1);

	}

	for(int m = l + 1; m < r; m++) {

		bool f = true;

		f &= calc(l, m);

		f &= calc(m, r);

		dp[l][r] |= f;

	}

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

	return dp[l][r];

}



int calc2(int l, int r) {

	if(dp2[l][r] != -1) return dp2[l][r];

	if(r - l <= 1) return dp2[l][r] = 0;

	if(dp[l][r]) return dp2[l][r] = r - l;

	int ret = 0;

	for(int m = l + 1; m < r; m++) {

		chmax(ret, calc2(l, m)+calc2(m, r));

	}

	return dp2[l][r] = ret;

}



int main(){

	int n;

	while(cin >> n && n) {

		w.clear();

		w.resize(n);

		rep(i, n) cin >> w[i];

		rep(i, 310) rep(j, 310) dp[i][j] = used[i][j] = false, dp2[i][j] = -1;

		REP(r, 1, n+1) rep(l, r) calc(l, r);

		cout << calc2(0, n) << endl;

		

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.7866427723488054x):
#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) REP(i,0,n)

#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)

#define repr(i, n) REPR(i, n, 0)

#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)

#define pb push_back

#define all(r) r.begin(),r.end()

#define rall(r) r.rbegin(),r.rend()

#define fi first

#define se second



typedef long long ll;

typedef vector<int> vi;

typedef vector<ll> vl;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;



const ll INF = 1e18;

const ll MOD = 1e9 + 7;

const double EPS = 1e-8;



template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}

template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}



vi w;



vector<vi> dp;



int calc(int l, int r) {

    if(dp[l][r] >= 0) return dp[l][r];

    auto& ret = dp[l][r];

    if(r - l == 1) return ret = 0;

    if(r - l == 2) {

        if(abs(w[l] - w[l+1]) <= 1) return ret = 2;

        else return ret = 0;

    }

    if(abs(w[l] - w[r-1]) <= 1) {

        int tmp = calc(l+1, r-1);

        if(tmp == r - l - 2) chmax(ret, tmp + 2);

    }

    for(int m = l + 1; m < r; ++m) {

        chmax(ret, calc(l, m) + calc(m, r));

    }

    return ret;

}



void solve() {

    int n;

    cin >> n;

    if(n == 0) exit(0);

    w.resize(n);

    rep(i, n) cin >> w[i];

    dp = vector<vi>(n+1, vi(n+1, -1));

    cout << calc(0, n) << endl;

}



int main(){

    while(1) solve();    

    return 0;

}