N = 10

def encode(i, j)
    (i << N) + j
end

def f(i, j)
    code = encode(i, j)
    cache = $memo[code]
    return cache if cache

    return 0 if i >= j
    if j == i + 1
        if ($a[i] - $a[j]).abs <= 1
            return $memo[code] = 2
        else
            return $memo[code] = 0
        end
    end
    if ($a[i] - $a[j]).abs <= 1 && f(i+1, j-1) == j - i - 1
        return $memo[code] = j - i + 1 
    end
    $memo[code] = (i..j-1).map {|k| f(i, k) + f(k+1, j)}.max
end

loop do
    n = gets.to_i
    break if n == 0
    $a = gets.split.map(&:to_i)
    $memo = {}
    p f(0, n-1)
end