#define TEST_MODE true

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; --i)
#define range(i, a, b, c) for (int i = a;             \
                               c > 0 ? i < b : i > b; \
                               i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define int long long

#ifdef LOCAL_ENV

#if TEST_MODE == true
const bool test = true;
#define dump(x) cerr << #x << " = " << (x) << endl
#else
const bool test = false;
#define dump(x) 
#endif

#else
const bool test = false;
#define dump(x) 
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = (int)1 << 60;
const ll INFL = (ll)1 << 60;
ll mod_n = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

vector<string> split(const string &str, char sep)
{
    vector<string> v;
    stringstream ss(str);
    string buffer;
    while (getline(ss, buffer, sep))
    {
        v.push_back(buffer);
    }
    return v;
}

template <class InputIterator>
int sum(InputIterator begin, InputIterator end)
{
    return accumulate(begin, end, 0ll);
}

void solve();

signed main()
{
    cout << fixed << setprecision(20);
    solve();
    while (true)
    {
        char s[MAX];
        if (scanf("%s", s) == EOF)
            break;
        int n = strlen(s);
        for (int i = n - 1; i > -1; i--)
        {
            ungetc(s[i], stdin);
        }
        solve();
    }
    return 0;
}

int n;
int w[MAX];
int done[305][305];
int dp[305][305];

void print_dp() {
    rep(i, n) {
        rep(j, n+1) {
            cout << setw(3) << dp[i][j];
        }
        cout << endl;
    }
}

int ans(int l, int r) {
    if (done[l][r]) {
        // if (test) {
        //     cout << "dp[" << l << "][" << r << "] : " << dp[l][r] << endl;
        // }
        return dp[l][r];
    }
    if (r - l == 0) return 0;
    if (r - l == 1) return 0; 
    if (l > r) return -INF;
    if (r > n) return -INF;
    if (r - l == 2) {
        done[l][r] = true;
        int res = 0;
        if (abs(w[l] - w[l+1]) < 2) res = 2;
        else res = 0;
        dp[l][r] = res;
        // if (test) {
        //     cout << "dp[" << l << "][" << r << "] : " << dp[l][r] << endl;
        // }
        return dp[l][r];
    }
    int res = 0;
    rep(width2, 2)
    {
        if (l + width2 > r)
            break;
        rep2(i, l, r)
        {
            if (i + width2 > r)
                break;
            if (i > r - width2)
                break;
            if (i == l && r - width2 == r) continue;
            // if (i + width2 == l && i + width2 == r)
            //     continue;
            // if (i + width + width2 * 2 == l)
            //     continue;
            int tmp =
                ans(l+width2, i) + ans(i, r-width2);

            if (tmp == r - l - width2 * 2)
            {
                rep(j, width2)
                {
                    if (abs(w[l] - w[r - 1]) < 2)
                    {
                        tmp += 2;
                    }
                    else
                        break;
                }
            }
            res = max(res, tmp);
        }
    }

    done[l][r] = true;
    dp[l][r] = res;
    // if (test) {
    //     cout << "dp[" << l << "][" << r << "] : " << dp[l][r] << endl;
    // }
    return dp[l][r];
}

void solve() {
    rep(i, 305) rep(j, 305) done[i][j] = false;
    rep(i, 305) rep(j, 305) dp[i][j] = 0;
    cin >> n;
    if (n == 0) exit(0);
    rep(i, n) cin >> w[i];
    cout << ans(0, n) << endl;
    if (test) print_dp();
}

Measured memory_bytes: 4616