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 <cstdio>
	#include <algorithm>
	#include <string>
	#include <cstring>
	#include <cctype>
	#include <cmath>
	#include <stack>
	#include <queue>
	#include <vector>
	#include <set>
	#include <map>
	#include <list>
	#include <stdio.h>
	#include <string.h>
	#include <cstdlib>
	#include <math.h>
	#include <bitset>
	#include <iterator>
	#include <iomanip>
	#include <sstream>
	#include <numeric>
	#include <cassert>
	using namespace std;
	int dx[5]={1,0,-1,0,0},dy[5]={0,1,0,-1,0};
	long long gcd(long long a,long long b){if(a%b==0){return b;}else return gcd(b,a%b);}
	long long lcm(long long a,long long b){if(a==0){return b;} return a/gcd(a,b)*b;}
	#define INF 2000000000
	#define MOD 1000000007
	#define sym cout<<"---------"<<endl;
	#define ll long long
	#define mk make_pair
	#define en endl
	#define RE return 0
	#define int ll
	#define P pair<int,int>
	
	int dp[305][305];

signed main(){
	int n;
	while(cin>>n&&n){
		int w[305];
		memset(dp, 0, sizeof(dp));
		for(int i=0; i<n; i++) cin>>w[i];
		
		for(int W=2; W<=n; W++){
			for(int l=0; l<n; l++){
				int r=l+W;
				
				if(dp[l+1][r-1]==W-2&&abs(w[l]-w[r-1])<=1) dp[l][r]=W;
				
				for(int mid=l; mid<=r; mid++){
					dp[l][r]=max(dp[l][r], dp[l][mid]+dp[mid][r]);
				}
			}
		}
		cout<<dp[0][n]<<en;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef pair<int,int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\n"

template<typename T> inline bool chmax(T &a, T b){if(a<b){a=b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if(a>b){a=b;return true;}return false;}
template<typename T> ostream& operator<<(ostream& s,const complex<T>& d) {return s<<"("<<d.real()<<", "<<d.imag()<< ")";}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const pair<T1,T2>& d) {return s<<"("<<d.first<<", "<<d.second<<")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& d){
	int len=d.size();
	rep(i,len){
		s<<d[i];if(i<len-1) s<<" ";
	}
	return s;
}
template<typename T> ostream& operator<<(ostream& s,const vector<vector<T>>& d){
	int len=d.size();
	rep(i,len){
		s<<d[i]<<endl;
	}
	return s;
}
template<typename T> ostream& operator<<(ostream& s,const set<T>& v){
	s<<"{ ";
	for(auto itr=v.begin();itr!=v.end();++itr) {
		if (itr!=v.begin()) {s<< ", ";}
		s<<(*itr);
	}
	s<<" }";
	return s;
}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const map<T1,T2>& m){
	s<<"{"<<endl;
	for(auto itr=m.begin();itr!=m.end();++itr){
		s<<" "<<(*itr).first<<" : "<<(*itr).second<<endl;
	}
	s<<"}"<<endl;
	return s;
}

const ll mod=1e9+7;
const ll inf=1e17;
const int INF=1e9;
const double PI=acos(-1);
const double EPS=1e-10;

int n;
vector<int> a(400);
vector<vector<int>> dp(400,vector<int>(400,-1));

int dfs(int l,int r){
	if(dp[l][r]!=-1) return dp[l][r];
	if(r-l==1){
		dp[l][r]=1;
		return dp[l][r];
	}
	if(r-l==2){
		if(abs(a[l]-a[l+1])<=1) dp[l][r]=0;
		else dp[l][r]=2;
		return dp[l][r];
	}
	int ret=INF;
	FOR(k,l+1,r){
		chmin(ret,dfs(l,k)+dfs(k,r));
	}
	if(dp[l+1][r-1]==0){
		if(abs(a[l]-a[r-1])<=1){
			chmin(ret,0);
		}else{
			chmin(ret,2);
		}
	}
	return dp[l][r]=ret;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	while(true){
		cin>>n;
		if(!n) break;
		rep(i,n){
			cin>>a[i];
		}
		rep(i,400)rep(j,400) dp[i][j]=-1;
		cout<<n-dfs(0,n)<<endl;
	}
}