#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <string.h>
#include <cmath>

using namespace std;
typedef long long i64;
typedef long double ld;
typedef pair<i64,i64> P;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);++(i))

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

		for(int W = 2;W <= n;W++)
		{
			//left
			for(int l = 0;l < n;l++)
			{
				int r = l + W - 1;
				if(r >= n) continue;

				if(dp[l + 1][r - 1] == W - 2 && abs(w[l] - w[r]) <= 1)
					dp[l][r] = W;

				for(int mid = l;mid <= r;mid++)
				{
					dp[l][r] = max(dp[l][r] , dp[l][mid] + dp[mid + 1][r]);
				}

			}

		}

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


