Find the Numbers which can be visited We have a number from 1 to N in a row.We chooses the first M numbers and moves them to the end of the queue. Now, we goes to the  at the first position in the row and mark it visited. we then notes this  index (say k) , and goes to the kth position in the row.If a number is visited we end our game.
We have to find the total number of numbers that are visited.
For Ex N= 5  m=2
3 4 5 1 2
we go to 1 number and from there 3rd Number i.e 5 and the 5th Number i.e 2 and then 2nd Number i.e  4 and i.e. 4 number i.e 1
Ex = 4 m=2
3 4 1 2
we go to 1 number i.e 3 and there to 3rd number i.e 1 and game stops.
 A: The appropriate mathematical tool for analyzing this situation is permutation theory, and in the language of permutation theory the problem is:

On the set $\{1,2,\ldots,N\}$ iterate the $N$-cycle $(1\;2\,\cdots\, N)$ $M$ times. In the resulting permutation, what is the length of the cycle that contains $1$?

By symmetry, all cycles in the result must be equally long, so the answer must be a divisor of $N$, and it comes down to how many different cycles the result contains. It turns out that the number of cycles is the greatest common divisor of $N$ and $M$, so the number you're looking for is
$$ \frac{N}{\gcd(N,M)} $$
which agrees with your examples: $\frac{5}{\gcd(5,2)}=\frac51=5$ and $\frac{4}{\gcd(2,4)}=\frac42=2$.

Another (?) way to look at it is modular arithmetic: What is the smallest (positive) number $k$ of jumps by $M$ one can do such that the total distance jumped -- that is, $kM$ -- is a multiple of $N$? This $k$ must be one that contributes all of the prime factors in $N$ that are not already in $M$ (and nothing more, or it wouldn't be the smallest one). Since the prime factors that $N$ and $M$ have in common are exactly the content of $\gcd(N,M)$, we can get the rest of them by dividing the common factors out of $N$, and again the result is
$$ \frac{N}{\gcd(N,M)} $$
