cho.sh
Notes
Loading...

Youngsik and Friends' Ball Toss

Time limit

2s

Memory limit

128 MB

Problem

Youngsik and his friends sit in a circle. The seats are numbered from 1 through N in clockwise order.

At first, the person in seat 1 receives the ball. Whenever a person receives the ball, they throw it to someone else according to how many times they have received the ball so far, including the ball they just received.

  • If that count is odd, they throw the ball to the L-th person clockwise from their current position.
  • If that count is even, they throw the ball to the L-th person counterclockwise from their current position.

The game ends as soon as any person has received the ball M times. Find the total number of throws made before the game ends.

Input

The first line contains three integers N, M, and L.

  • 3 <= N <= 50
  • 1 <= M <= 50
  • 1 <= L <= N - 1

Output

Print one integer: the total number of times the ball is thrown.

Hint

In the first visible test, seat 1 receives the ball first. The ball then moves through seats 1 -> 3 -> 5 -> 2 -> 4 -> 1 -> 4 -> 2 -> 5 -> 3 -> 1. Seat 1 receives the ball for the third time, so the game ends after 10 throws.