#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(),x.end()
#define dbg(x) cout<<#x<<":"<<x<<endl
#define int long long
#define MOD 1000000007
typedef pair<int,int> P;
typedef pair<int,P> PP;
int n,w[333];
int dp[333][333];

int rec(int l,int r){
  if(dp[l][r]!=-1)return dp[l][r];
  dp[l][r]=0;
  for(int i=l+1;i<r;i++){
    int res=0;
    int suml=rec(l+1,i);
    int sumr=rec(i+1,r);
    if(suml==i-l-1&&abs(w[l]-w[i])<2)res+=2;
    dp[l][r]=max(dp[l][r],res+suml+sumr);
  }
  return dp[l][r];
}

signed main(){
  while(1){
  cin>>n;
  if(n==0)break;
  for(int i=0;i<n;i++)cin>>w[i];
  fill_n(*dp,333*333,-1);
  for(int i=0;i<n;i++){
    dp[i][i]=0;
    dp[i][i+1]=0;
  }
  cout<<rec(0,n)<<endl;
  }
  return 0;
}

