#include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

using ll = long long;

int main() {
    int n;
    cin >> n;
    while (n != 0) {
            
        vector<int> w(n);
        rep(i, n) cin >> w[i];
        // dp[i][j] : iからj-1までのブロックから取れる最大値
        vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
        for (int k = 2; k <= n; k++) {
            for (int i = 0; i + k <= n; i++) {
                if (dp[i + 1][i + k - 1] == k - 2 && abs(w[i] - w[i + k - 1]) < 2) {
                    dp[i][i + k] = k;
                }
                for (int j = i; j <= i + k; j++) {
                    dp[i][i + k] = max(dp[i][i + k], dp[i][j] + dp[j][i + k]);
                }
            }
        }
        cout << dp[0][n] << "\n";
        cin >> n;
    }
}
