#include <bits/stdc++.h>
#define FOR(i,k,n) for(int i=(k);i<int(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) begin(x),end(x)

using namespace std;

int main() {
  while(1){
    int n;
    cin>>n;
    if(!n)break;
    vector<int> w(n);
    REP(i,n)cin>>w[i];
    vector<vector<int>> dp(n, vector<int>(n+1, 0));
    REP(i,n-1) {
      if (abs(w[i]-w[i+1]) <= 1) dp[i][2] = 2;
    }
    FOR(l,3,n+1) REP(i,n) {
      if (i+l > n) break;
      FOR(k,1,l) {
        dp[i][l] = max(dp[i][l], dp[i][k] + dp[i+k][l-k]);
      }
      if (dp[i+1][l-2] == l-2 && abs(w[i] - w[i+l-1]) <= 1) {
        dp[i][l] = l;
      }
    }
    cout << dp[0][n] << endl;
  }
  return 0;
}