#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<string.h>
#include<string>
#include<functional>
#include<set>
using namespace std;

#define INF 1<<21
#define DIV 1000000007
#define MAX 250000


int main(){
  int n;
  while (cin >> n, n) {
	int w[305];
	int dp[305][305];
	memset(dp, 0, sizeof(dp));
	for (int i = 0; i < n; i++) {
	  cin >> w[i];
	}

	for (int i = 1; i < n; i++) {
	  for (int j = 0; j + i < n; j++) {


		for (int k = j; k < j + i; k++) {
		  dp[j][j + i] = max(dp[j][j + i], dp[j][k] + dp[k + 1][j + i]);
		}


		if (dp[j + 1][j + i - 1] == i - 1 && abs(w[j] - w[j + i])<2)dp[j][j + i] = i + 1;


	  }
	}
	cout << dp[0][n - 1] << endl;
  }
  return 0;
}