#include <bits/stdc++.h>
                                        #include<iostream>
                                        #include<cstdio>
                                        #include<vector>
                                        #include<queue>
                                        #include<map>
                                        #include<cstring>
                                        #include<string>
                                        #include <math.h>
                                        #include<algorithm>
                                    //    #include <boost/multiprecision/cpp_int.hpp>
                                        #include<functional>
                                #define int long long
                                        #define inf  1000000007
                                        #define pa pair<int,int>
                                        #define ll long long
                                        #define pal pair<double,pa>
                                        #define ppa pair<int,int>
                                        #define ppap pair<int,pa>
                                        #define ssa pair<string,int>
                                        #define  mp make_pair
                                        #define  pb push_back
                                        #define EPS (1e-10)
                                        #define equals(a,b) (fabs((a)-(b))<EPS)
                                  #define VI vector<int>
                                        using namespace std;
                                        
                                  class pas{
                                    	public:
                                    	int x,y;
                                    	pas(int x=0,int y=0):x(x),y(y) {}
                                    	pas operator + (pas p) {return pas(x+p.x,y+p.y);}
                                    	pas operator - (pas p) {return pas(x-p.x,y-p.y);}
                                    	pas operator * (int a) {return pas(x*a,y*a);}
                                    	pas operator / (int a) {return pas(x/a,y/a);}
                                    //	double absv() {return sqrt(norm());}
                                    	int norm() {return x*x+y*y;}
                                    	bool operator < (const pas &p) const{
                                   		return x != p.x ? x<p.x: y<p.y;
                                   	}
                                  //		bool operator < (const pas &p) const{
                                    //		return y != p.y ? y<p.y: x<p.x;
                                    //	}
                                    	bool operator == (const pas &p) const{
                                    		return x==p.x && y==p.y;
                                    	}
                                    };
int dp[310][310];
int dp2[310][310];
int a[310];
         signed main(){
	
	int n;
         	while(1){
         	cin>>n;
         		if(n==0) return 0;
         		for(int i=0;i<310;i++)for(int j=0;j<310;j++)dp[i][j]=0,dp2[i][j]=0;
         		for(int i=0;i<310;i++)a[i]=0;
         		
         		for(int i=0;i<n;i++)cin>>a[i];
         		
         		for(int i=0;i<n-1;i++)if(abs(a[i]-a[i+1])<=1)dp[i][i+2]=1;
         		
         		for(int i=0;i<n;i++)dp[i][i]=1;
         		for(int d=4;d<=n;d+=2){
         			for(int i=0;i+d<=n;i++){
         				int j=i+d;
         				bool bo=false;
         				for(int k=i+2;k<=j-2;k+=2)if(dp[i][k] && dp[k][j]) bo=1;
         				if(dp[i+1][j-1] && abs(a[i]-a[j-1])<=1) bo=1;
         				if(bo) dp[i][j]=1;
         			}
         		}
         		
         		
         		for(int i=0;i<n-1;i++)if(abs(a[i]-a[i+1])<=1)dp2[i][i+2]=2;
         		
         		for(int d=3;d<=n;d++){
         			for(int i=0;i+d<=n;i++){
         				int j=i+d;
         				if(dp[i][j]){
         					dp2[i][j]=j-i;
         					continue;
         				}
         				int e=0;
         				for(int k=i+1;k<=j-1;k++)e=max(e,dp2[i][k]+dp2[k][j]);
         				dp2[i][j]=e;
         				
         			}
         			
         		}
         		cout<<dp2[0][n]<<endl;
         	}
         
         	//cout<<ans<<endl;
           	return 0;
           }


Measured memory_bytes: 4576