fun main(args:Array<String>?): Unit {
    while(true){
        val n = readLine()!!.trim().toInt()
        if (n == 0) return
        val weights = readLine()!!.trim().split(' ').map(String::toInt).toIntArray()
        println(solve(weights))
    }
}
fun solve(weights: IntArray): Int {
    val memo = Array(weights.size){i -> IntArray(weights.size - i){-1}}
    fun count(from: Int, until: Int): Int {
        if (until - from <= 1) return 0
        if (memo[from][until - from - 1] == -1) {
            memo[from][until - from - 1] = Math.max(count(from + 1, until), (from + 1 until until).filter{mid -> Math.abs(weights[from] - weights[mid]) <= 1 && count(from + 1, mid) == mid - from - 1}.map{mid -> mid - from + 1 + count(mid + 1, until)}.max() ?: 0)
        }
        return memo[from][until - from - 1]
    }
    return count(0, weights.size)
}
