Description

A ray starts from southwest corner of a square room with mirrors. Given p (room side) and q (receptor location), find which receptor is hit.

Examples

Input:p = 2, q = 1
Output:2
Explanation:

Laser hits receptor 2.

Input:p = 4, q = 3
Output:0
Explanation:

The laser bounces multiple times between the mirrors. After reflecting off the walls, it eventually reaches receptor 0 on the left wall.

Input:p = 1, q = 2
Output:1
Explanation:

With equal vertical distance but shorter horizontal distance, the laser reaches receptor 1 on the right wall after bouncing vertically.

Constraints

  • 1 ≤ p ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!