#include <bits/stdc++.h>
using namespace std;

int main(){
    
    for(int n; cin >> n, n;){
        vector<int>w(n);
        vector<vector<int> >dp(n, vector<int>(n, 0));
        for(int i = 0; i < n; i++)cin >> w[i];
        for(int l = 0; l + 1 < n; l++){
            if(abs(w[l] - w[l + 1]) <= 1){
                dp[l][l + 1] = 2;
            }
        }
        for(int i = 2; i <= n; i++){
            for(int l = 0; l + i < n; l++){
                int r = l + i;
                if(abs(w[l] - w[r]) <= 1 && dp[l + 1][r - 1] == r - l - 1){
                    dp[l][r] = r - l + 1;
                }
                for(int p = l; p < r; p++){
                    dp[l][r] = max(dp[l][r], dp[l][p] + dp[p + 1][r]);
                }
            }
        }
        cout << dp[0][n - 1] << endl;
    }

    return 0;
}
