#include <bits/stdc++.h>

#define DEBUG(C) cerr << #C << " = " << C << endl

using namespace std;

const int MAX = 333;
int w[MAX];
int dp[MAX][MAX];

bool solve() {
  int n;
  scanf("%d", &n);
  if (n == 0) {
    return false;
  }
  memset(w, 0, sizeof(w));
  for (int i = 0; i < n; i++) {
    scanf("%d", w + i);
  }
  memset(dp, 0, sizeof(dp));
  
  for (int len = 2; len <= n; len++) {
    for (int l = 0; l <= n - len; l++) {
      const int r = l + len;
      for (int m = l; m <= r; m++) {
        dp[l][r] = max(dp[l][r], dp[l][m] + dp[m][r]);
      }
      if (abs(w[l] - w[r - 1]) <= 1 && dp[l + 1][r - 1] == (r - 1) - (l + 1)) {
        dp[l][r] = r - l;
      }
    }
  }
  
  printf("%d\n", dp[0][n]);
  return true;
}

int main() {
  while (solve()) {}
  return 0;
}