Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 10000000
	maxBufSize     = 100000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	a, _ := strconv.Atoi(sc.Text())
	return int(a)
}
func nextString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextIntSlice(sc *bufio.Scanner, n int) (a []int) {
	a = make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = nextInt(sc)
	}
	return a
}

var dp [][]int

func initDP(n int) {
	dp = make([][]int, n+1)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n+1)
		for j := 0; j < len(dp[0]); j++ {
			dp[i][j] = -1
		}
	}
}

func resolve() {
	ans := []int{}
	for true {
		n := nextInt(sc)
		if n == 0 {
			break
		}
		as := nextIntSlice(sc, n)
		initDP(n)
		_rec(as, n, 0, n)
		ans = append(ans, dp[0][n])
	}
	for _, v := range ans {
		fmt.Println(v)
	}
	return
}

func _rec(as []int, n, l, r int) int {
	if dp[l][r] > -1 {
		return dp[l][r]
	}
	if r-l == 1 { // prevent to hit daruma
		dp[l][r] = 0
		return dp[l][r]
	}
	if r-l == 2 {
		if abs(as[l]-as[r-1]) <= 1 {
			dp[l][r] = 2
		} else {
			dp[l][r] = 0
		}
		return dp[l][r]
	}
	if abs(as[l]-as[r-1]) <= 1 {
		v := _rec(as, n, l+1, r-1)
		// if we can remove internal blocks
		if (r - 1 - (l + 1)) == v {
			dp[l][r] = v + 2
			return dp[l][r]
		} else {
			dp[l][r] = v
		}
	}
	for i := l + 1; i < r; i++ {
		v := _rec(as, n, l, i) + _rec(as, n, i, r)
		if dp[l][r] < v {
			dp[l][r] = v
		}
	}
	return dp[l][r]
}

func abs(v int) int {
	if v < 0 {
		return -v
	}
	return v
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
vector<int> a;
vector<vector<int>> dp;
int rec(int l, int r) {
  if(dp[l][r] >= 0) return dp[l][r]; // memo
  if(r - l < 2) return dp[l][r] = 0;
  if(abs(a[l] - a[r-1]) <= 1) { // all clear
    if(rec(l+1, r-1) == (r-1) - (l+1)) {
      return dp[l][r] = r - l; 
    }
  }
  int res = 0;
  for (int i = l+1; i < r; ++i) {
    chmax(res, rec(l, i) + rec(i, r));
  }
  return dp[l][r] = res;
}

int main()
{
  vector<int> res;
  while(1) {
    int n; cin >> n;
    if(n == 0) break;
    a.resize(n);
    rep(i,n) cin >> a[i];
    dp.assign(n+1, vector<int>(n+1, -1));
    res.push_back(rec(0, n));
  }
  rep(i,res.size()) cout << res[i] << "\n";
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

int main() {
  unsigned int N = 0;
  vector<int> num;
  int w = 0;

  cin >> N;
  while(N != 0) {
    for(unsigned int i = 0; i < N; i++) {
      cin >> w;
      num.push_back(w);
    }

    while(true) {
      unsigned int i;
      for(i = num.size()-1; i > 0; i--)
	if(num.at(i)-num.at(i-1) >= -1 && num.at(i)-num.at(i-1) <= 1)
	  break;

      if(i == 0)
	break;
      num.erase(num.begin()+i);
      num.erase(num.begin()+i-1);
      
      if(num.empty())
	break;
    }

    cout << N-num.size() << endl;

    num.clear();

    cin >> N;
  }


  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
vector<int> w;
int dp[333][333];

int solve(int l, int r) {
  if (r - l <= 1) return 0;
  if (r - l == 2) {
    if (abs(w[l + 1] - w[l]) <= 1) {
      return 2;
    } else {
      return 0;
    }
  }
  if (dp[l][r] != -1) return dp[l][r];
  if (abs(w[r - 1] - w[l]) <= 1 && solve(l + 1, r - 1) == r - l - 2) {
    chmax(dp[l][r], r - l);
  }

  for (int i = l + 1; i < r; i++) {
    chmax(dp[l][r], solve(l, i) + solve(i, r));
  }
  return dp[l][r];
}

int main() {
  while (cin >> n, n) {
    w.assign(n, 0);
    for (int i = 0; i < n; i++) {
      cin >> w[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        dp[i][j] = -1;
      }
    }
    cout << solve(0, n) << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; cin >> x;
#define int2(x, y) Int(x); Int(y);
#define int3(x, y, z) Int(x); int2(y, z);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
#define Decimal fixed << setprecision(10)

//int dxy[5] = {0, 1, 0, -1, 0}; 

int n;
int data[300];
int dp[300][301]; // hankai
int score[300][301];

int solve(int st, int en)
{
    //cout << "call : " << st << " " << en << endl;;
    if ( dp[st][en] ) {
        //cout << " ret1 : 0" << endl;
        return score[st][en] = dp[st][en] = 0;
    }
    if ( score[st][en] ) {
        //cout << " ret2 : " << score[st][en] << endl;
        return score[st][en];
    }
    if (en - st < 2) {
        //cout << " ret3 : " << en - st << endl;
        return score[st][en] = en - st;
    }
    int ret = en - st;
    for (int i = st+2; i < en; i++) {
        if (dp[st][i]) {
            ret = min(solve(i, en), ret);
        }
    }
    return score[st][en] = min(ret, 1+solve(st+1, en));
    //score[st][en] = min(ret, 1+solve(st+1, en));
    //cout << score[st][en] << endl;
    //return score[st][en];
}

void make(int st, int en)
{
    //cout << "call : " << st << " " << en << endl;
    if (en - st < 2) return;

    if (0 <= st - 1 && en + 1 <= n && abs(data[st-1]-data[en]) < 2)
        dp[st-1][en+1] = 1;

    for (int i = max(0LL, st - (en - st)); i < st; i++)
        if (dp[i][st]) dp[i][en] = 1;

    for (int i = en+1; i < min(n+1, en + (en - st)); i++)
        if (dp[en][i]) dp[st][i] = 1;

}

signed main() 
{ 
    std::ios::sync_with_stdio(false); 
    std::cin.tie(0); 

    cin >> n;
    while ( n ) {
        memset(data, 0, sizeof(data));
        memset(dp, 0, sizeof(dp));
        memset(score, 0, sizeof(score));
        rep(i, n) cin >> data[i];

        rep(i, n-1) if (abs(data[i] - data[i+1]) < 2) dp[i][i+2] = 1;
        /*
        rep(i, n) {
            Rep(j, n+1) {
                cout << dp[i][j] << " ";
            }
            cout << endl;
        }
        cout << "============" << endl;
        */

        for (int i = 2; i < n; i++) {
            for (int st = 0; st < n - i + 1; st++) {
                int en = st + i;
                if (dp[st][en]) make(st, en);
            }
        }

        /*
        rep(i, n) {
            Rep(j, n+1) {
                cout << dp[i][j] << " ";
            }
            cout << endl;
        }
        */

        cout << n - solve(0, n) <<endl;
        cin >> n;
    }

    return 0; 
}