#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> w;
vector<vector<int>> dp;

int rec(int l, int r)
{
  if (~dp.at(l).at(r))
  {
    return dp.at(l).at(r);
  }
  if (l == r)
  {
    return dp.at(l).at(r) = 0;
  }
  if (l + 1 == r)
  {
    if (abs(w.at(l) - w.at(r)) <= 1)
    {
      return dp.at(l).at(r) = 2;
    }
    return dp.at(l).at(r) = 0;
  }
  int tmp = 0;
  if (abs(w.at(l) - w.at(r)) <= 1 && rec(l + 1, r - 1) == r - l - 1)
  {
    tmp = r - l + 1;
  }
  for (int m = l; m < r; m++)
  {
    tmp = max(tmp, rec(l, m) + rec(m + 1, r));
  }
  return dp.at(l).at(r) = tmp;
}

int main()
{
  vector<int> ans;
  while (1)
  {
    cin >> n;
    if (!n)
    {
      break;
    }
    w.resize(n);
    for (int i = 0; i < n; i++)
    {
      cin >> w.at(i);
    }
    dp.resize(n, vector<int>(n));
    dp.assign(n, vector<int>(n, -1));
    ans.push_back(rec(0, n - 1));
  }
  for (int i = 0; i < ans.size(); i++)
  {
    cout << ans.at(i) << endl;
  }
}
