/*
 * daruma_otoshi_2016D.cpp
 * ICPC2016 Domestic D
 *
 *  Created on: 2017/07/13
 *      Author: LitMc
 */

//#define MYDEBUG
#include <bits/stdc++.h>

#ifdef MYDEBUG
#define dbp(x) cout<<#x<<": "<<x<<endl
#define dbp2(x,y) cout<<#x<<","<<#y<<": "<<x<<","<<y<<endl
#define dbp3(x,y,z) cout<<#x<<","<<#y<<","<<#z<<": "<<x<<","<<y<<","<<z<<endl
#define dbp4(w,x,y,z) cout<<#w<<","<<#x<<","<<#y<<","<<#z<<": "<<w<<","<<x<<","<<y<<","<<z<<endl
#define ifcin(x) std::ifstream cin(x)
#else
#define dbp(x)
#define dbp2(x,y)
#define dbp3(x,y,z)
#define dbp4(w,x,y,z)
#define ifcin(x)
#endif
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for(int i=from; i<to; ++i)
#define REP(i, from, to) for(int i=from; i<=to; ++i)
#define EPS = 1e-14;
using std::vector;
using std::cout;
using std::cin;
using std::endl;
using std::max;
using std::min;
using std::swap;
using std::string;
using std::fill;
using std::pair;
using std::sort;
using std::reverse;
using std::pair;
using std::greater;
using std::priority_queue;
using std::ostream;

template<typename T>
ostream& operator<<(ostream& out, const vector<vector<T> >& v) {
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i] << endl;
  }
  return out;
}

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last) {
      out << ",";
    }
  }
  out << "]";
  return out;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

typedef pair<int, int> P;
bool cmp(const P &p1, const P &p2) {
  if (p1.first == p2.first) {
    return p1.second > p2.second;
  } else {
    return p1.first < p2.first;
  }
}

static const int MAX_N = 310;
int n;
bool can[MAX_N][MAX_N];
int dp[MAX_N][MAX_N];
//can[i][j]: w[i]??????w[j]?????§???????????????
//can[i][j] = 1 (if |w[i]-w[j]| <= 1)
//can[i][j] = 1 (if can[i][k] && can[k+1][j])
//dp[i][j]: [i, j]???????????§????????????????????§?????°

vector<int> w;
void init(int n) {
  fill(all(w), 0);
  memset(can, 0, sizeof(can));
  memset(dp, 0, sizeof(dp));
}

bool near(int i, int j) {
  return abs(w[i] - w[j]) <= 1;
}

void debug_can() {
  printf("w[ i]: ");
  rep(i,0,n)
  {
    printf("%2d ", i);
  }
  printf("\n");
  rep(i,0,n)
  {
    printf("w[%2d]: ", i);
    rep(j,0,n)
    {
      printf("%2d ", can[i][j]);
    }
    printf("\n");
  }
}

void doDP_can() {
  rep(i,0,n)
  {
    can[i][i] = false;
  }
  for (int i = 0; i + 1 < n; ++i) {
    if (near(i, i + 1)) {
      can[i][i + 1] = true;
    }
  }
  for (int width = 3; width <= n; ++width) {
    for (int i = 0, j = i + width - 1; j < n; ++i, ++j) {
      if (near(i, j) && can[i + 1][j - 1]) {
        can[i][j] = true;
        continue;
      }
      for (int k = i; k + 1 <= j; ++k) {
        if (can[i][k] && can[k + 1][j]) {
          can[i][j] = true;
          break;
        }
      }
    }
  }
//  debug_can();
}

//dp[i][j]: [i, j]???????????§????????????????????§?????°
//dp[i][j] = j - i + 1 (if can[i][j]==true)
//dp[i][j] = max_k (dp[i][k] + dp[k+1][j])
//????????????[i, j]????????????????±?????????¨???, ????????????????????????????????????????????????????????£???????????°DP??§??????
//width = 2, 3, ... n
//???width?????????[i, j]
void doDP_count() {
  for (int width = 2; width <= n; ++width) {
    for (int i = 0, j = i + width - 1; j < n; ++i, ++j) {
      if (can[i][j]) {
        dp[i][j] = j - i + 1;
        continue;
      }
      int maxi = 0;
      for (int k = i; k + 1 <= j; ++k) {
        maxi = max(maxi, dp[i][k] + dp[k + 1][j]);
      }
      dp[i][j] = maxi;
    }
  }
}

void solve() {
  while (cin >> n, n) {
    w = vector<int>(n);
    init(n);
    rep(i,0,n)
    {
      cin >> w[i];
    }
    doDP_can();
    doDP_count();
    cout << dp[0][n - 1] << endl;
  }
}

int main() {
  solve();
}

Measured memory_bytes: 3552