#include <iostream>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <map>
#include <math.h>
using namespace std;

int n, w[310];
int dp[310][310];

int maxx(int i, int j) {
    if (i > j) return i;
    else return j;
}

bool solve(int i, int j) {
    if (dp[i][j] >= 0) return dp[i][j];
    else {
        if (i >= j || (j - i) % 2 == 0 || i >= n || j >= n) {
            dp[i][j] = 0;
            return dp[i][j];
        }
        if (j - i == 1) {
            if (abs(w[i] - w[j]) <= 1) dp[i][j] = 1;
            else dp[i][j] = 0;
            return dp[i][j];
        }
        
        if (solve(i + 1, j - 1) == 1) {
            if (abs(w[i] - w[j]) <= 1) dp[i][j] = 1;
            else dp[i][j] = 0;
        }
        
        for (int k = 2; j - k > 0; k += 2) {
            for (int l = 2; i + l < j; l += 2) {
                if (solve(i, j - k) == 1 && solve(i + l, j) == 1 && k + l == j - i + 1) {
                    dp[i][j] = 1;
                    return dp[i][j];
                }
            }
        }
        
        
        if (dp[i][j] == -1) dp[i][j] = 0;
        
        return dp[i][j];
    }
}
    
int main(void){
    while (1) {
        cin >> n;
        if (n == 0) return 0;
        for (int i = 0; i < n; i++) cin >> w[i];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) dp[i][j] = -1;
        }
        
        
        int min = 0, max = 0, ans = 0;
        int min2 = 0, max2 = 0, ans2 = 0;
        int min3 = 0, max3 = 0, ans3 = 0;
        for (int i = 0; i < n; i++) {
            bool mmm = true, mmm2 = true, mmm3 = true;
            if (min > i) mmm = false;
            if (min2 > i) mmm2 = false;
            if (min3 > i) mmm3 = false;
            for (int j = n - 1; j >= 0; j--) {
                if (!mmm && !mmm2 && mmm3 && solve(i, j) == 1 && solve(i, j + 2) != 1 && min3 <= i) {
                        max3 = j;
                        mmm3 = false;
                    //cout << "m3 " << max2 << endl;
                }
                if (!mmm && mmm2 && solve(i, j) == 1 && solve(i, j + 2) != 1 && min2 <= i) {
                        max2 = j;
                        mmm2 = false;
                    //cout << "m2 " << max2 << endl;
                }
                if (mmm && solve(i, j) == 1 && min <= i) {
                    max = j;
                    mmm = false;
                }
            }
            /*
            if (!mmm2 && mmm3) {
                max3 = max2;
                mmm3 = false;
            }
            */
            if (max > min) {
                ans += (max - i) + 1;
                min = max + 1;
                max = min;
            }
            else {
                min = maxx(min, i);
                max = min;
            }
            
            if (max2 > min2) {
                ans2 += (max2 - i) + 1;
                min2 = max2 + 1;
                max2 = min2;
            }
            else {
                min2 = maxx(min2, i);
                max2 = min2;
            }
            
            if (max3 > min3) {
                ans3 += (max3 - i) + 1;
                min3 = max3 + 1;
                max3 = min3;
            }
            else {
                min3 = maxx(min3, i);
                max3 = min3;
            }
            
            if (max == max2 && max2 == max3) {
                ans = maxx(ans, ans2);
                ans = maxx(ans, ans3);
                ans2 = ans;
                ans3 = ans;
                //cout << "equal" << endl;
            }
            
            else if (max2 == max3 + 1) {
                ans2 = maxx(ans2, ans3);
                ans3 = ans2;
                //cout << "equalf" << endl;
            }
            
            
            //cout << "min " << min << " max " << max 
           //    << " min2 " << min2 << " max2 " << max2 
            //    <<  " min3 " << min3 << " max3 " << max3 << endl; 
            
        }
        
        
        
        cout << ans << endl;
        
        /*
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << solve(i, j) << " ";
            }
            cout << endl;
        }
        */
        
    }
    
    
}


Measured memory_bytes: 3484