Sending to the end of the queue

People are waiting in line to enter a zoo. We know each person's name as they have been registered before entering the queue. Yet, someone started to behave strangely and push his neighbors, and the security decided to move him to the very end of the queue. Given the initial queue and the number of the person to move to the very end, you are asked to print out the resulting queue.
The first line of the input contains space-separated names that form the queue. The second line of the input contains a single integer - the number of the person (starting from the front) who behaves strangely. The numbering starts from 1.
The program should print the resulting queue, separating names by a space.
Input
Output
Anna Mary Karen Bob Simon Danny John Lucy 6
Anna Mary Karen Bob Simon John Lucy Danny
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue