while true
    n = gets.to_i
    break if n == 0
    jiro = gets.chomp.split.map(&:to_i)
    nokori, turn = 32, true
    while nokori > 0
        if turn
            nokori -= (nokori - 1) % 5
        else
            nokori = [nokori - jiro[0], 0].max
            jiro.rotate!(1)
        end
        turn = !turn
        puts nokori
    end
end
