Problem 856
Mirror Reflection
Medium · 20 pts · Math, Geometry, Number Theory
Problem
A square room has mirrors on all four walls and side length P. Receptors sit in three corners: receptor 0 at the bottom-right, 1 at the top-right and 2 at the top-left. A laser starts in the bottom-left corner and first hits the right wall at distance Q from the bottom. It keeps reflecting until it reaches a receptor. Unfold the reflections: the beam travels to the first height L = lcm(P, Q); the parity of L / P and L / Q decides the receptor.
Input
A single line with two integers P and Q.
Output
Print the number of the receptor the laser meets first.
Constraints
1 <= Q <= P <= 1000
Example input
2 1
Example output
2
Explanation
With P = 2 and Q = 1, the beam hits the right wall halfway up, reflects, and reaches the top-left corner, which is receptor 2.