/*
 * Copyright (C) 2016 Hiroyasu Kamo
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * ACM-ICPC 2016 Japan Domestic Contest
 *  Problem D  Daruma Otoshi
 *
 *	Author: Hiroyasu Kamo <wd@ics.nara-wu.ac.jp>
 */

#include <cstdlib>
#include <algorithm>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <iostream>
using std::size_t;
using std::abs;
using std::pair;
using std::make_pair;
using std::vector;
using std::set;
using std::map;
using std::cin;
using std::cout;
using std::endl;

set<pair<int, int> >
daruma_aux1(const vector<int> &ws)
{
	set<pair<int, int> >	s0;
	for (int i = 0; i + 1 < ws.size(); ++ i) {
		if (abs(ws[i] - ws[i + 1]) <= 1) {
			s0.insert(make_pair(i, i + 2));
		}
	}
	for (int len = 2; len < ws.size(); len += 2) {
		for (int i = 0, j = len; j <= ws.size(); ++ i, ++ j) {
			if (s0.find(make_pair(i, j)) != s0.end()) {
				if (i > 0 && j < ws.size() && abs(ws[i - 1] - ws[j]) <= 1) {
					s0.insert(make_pair(i - 1, j + 1));
				}				
				for (int k = i - 2; k >= 0 && i - k <= len; k -= 2) {
					if (s0.find(make_pair(k, i)) != s0.end()) {
						s0.insert(make_pair(k, j));
					}
				}
				for (int k = j + 2; k <= ws.size() && k - j < len; k += 2) {
					if (s0.find(make_pair(j, k)) != s0.end()) {
						s0.insert(make_pair(i, k));
					}
				}
			}
		}
	}
	return std::move(s0);
}

int
daruma_aux2(const set<pair<int, int> > &s)
{
	map<pair<int, int>, int>	f;
	int	n = 0;
	for (auto p = s.rbegin(); p != s.rend(); ++ p) {
		int	k = p->second - p->first;
		const auto	q = f.lower_bound(make_pair(p->second, p->second));
		if (q != f.end()) {
			k += q->second;
		}
		if (n < k) {
			n = k;
		}
		f.insert(f.begin(), make_pair(*p, n));
	}
	return n;
}

int
daruma(const vector<int> &ws)
{
	const set<pair<int, int> >	s = daruma_aux1(ws);
	const int	n = daruma_aux2(s);
	return n;
}

int
main()
{
	for (int n; cin >> n && n > 0; ) {
		vector<int>	ws;
		for (int i = 0; i < n; i ++) {
			int	w;
			cin >> w;
			ws.push_back(w);
		}
		cout << daruma(ws) << endl;
	}
	return 0;
}
