loop do
    n = gets.to_i
    break if n == 0
    a = gets.split.map(&:to_i)
    table = (0..n-1).map { [0] * n }
    (0..n-2).each do |i|
        table[i][i+1] = 2 if (a[i] - a[i+1]).abs <= 1
    end
    (2..n-1).each do |d|
        (0..n-1-d).each do |l|
            r = l + d
            x = table[l+1][r-1]
            table[l][r] = x + 2 if (a[l] - a[r]).abs <= 1 && x == r - l - 1
            (l..r-1).each do |i|
                m = table[l][i] + table[i+1][r]
                table[l][r] = m if m > table[l][r]
            end
        end
    end
    p table[0][n-1]
end