The Bovine Fire Drill

Time limit1sMemory limit128 MB

Summary
Simulate cows on a circular ring of N chairs, each moving clockwise by its own number, and report the cow that ends the drill.
Level

Medium4 of 10

Topics
Simulation, Implementation, Array
Solved
No attempts yet

Problem

NN cows (3≤N≤2503 \le N \le 250) sit in a perfect circle around a campfire. For convenience the cows are numbered 11 through NN, and the chairs are likewise numbered 11 through NN; initially cow ii sits in chair ii.

After one of Farmer John's stories ends, he suggests a Bovine Fire Drill.

In a Bovine Fire Drill, only one cow moves at a time. When it is cow ii's turn, she stands up and walks clockwise to the chair that is ii positions away from the chair she is sitting in. (For example, if it is cow 33's turn, she moves three chairs clockwise from where she is.)

When cow ii reaches her new chair, she taps the cow sitting there on the shoulder. That cow stands up to make room, cow ii sits down, and the cow who just stood up takes the next turn, moving in the same way.

The process continues until one of the following happens:

  • a cow arrives at the empty chair, or
  • a cow is asked to move for a second time; that is, a cow who has already moved once is tapped again.

Because cow 11 always moves first, the empty chair is always the chair where cow 11 originally sat.

Thanks to the properties of whole numbers the drill usually ends early, so it is rare for every cow to take part. The last cow to move — whether she ends the drill by sitting in the empty chair 11 or by tapping a cow that has already moved — receives a special treat of tender spring grass.

Help Farmer John figure out in advance which cow will receive the treat.

For example, suppose five cows are sitting around the campfire. Below, * marks the empty chair.

  2   -   3
 (         )
  1 - 5 - 4

First, cow 11 walks one chair and taps cow 22, who stands up.

    2
 1   -   3
 (         )
  * - 5 - 4

Cow 22 walks two chairs and taps cow 44, who begins her journey.

  1  -    3
 (         )
  * -  5 - 2
          4

Cow 44 walks four chairs and taps cow 33.

             3
  1   -    4
 (         )
  * -  5 - 2

Finally, cow 33 walks three chairs to the empty chair 11, which ends the drill.

  1   -    4
 (         )
  3 -  5 - 2

Cow 33 receives tender spring grass as the other cows clap and cheer.

Input

The first line contains the integer NN.

Output

Print the number of the cow who ends the drill.

Examples1

  1. Example 1

    Input
    5
    
    Expected output
    3