Under construction

Still being built. Things may change

An Important Scientific Number

Interview

Time limit2sMemory limit1024 MB

Summary
Given gear sizes a and b, find the smallest non-negative x so that b divides a+x and a divides b+x.
Level

Medium4 of 10

Topics
Number theory, Math
Solved
No attempts yet

Statement

Ping was assembling his very important new invention when he realized he had made a mistake in one of the formulas, and the corresponding gear may have been assembled incorrectly.

After looking closely at the part and fixing the formula, Ping noticed that the part currently has two gears with aa and bb teeth. To work correctly, the gears must have a+xa + x and b+xb + x teeth, where xx is a non-negative integer such that a+xa + x is divisible by bb and b+xb + x is divisible by aa.

Ping is very tired, so he asks you to find a non-negative xx that satisfies these conditions. Ping does not like large gears, so among all suitable values of xx, choose the smallest one.

Input

A single line contains two space-separated integers aa and bb, the sizes of the gears in the part (1≤a,b≤1091 \le a, b \le 10^9).

Output

Print one integer xx, the smallest non-negative value that satisfies the conditions. This is the minimum number of teeth the gears are missing for the invention to work correctly.

Examples4

  1. Example 1

    Input
    10 5
    
    Expected output
    5
    
  2. Example 2

    Input
    8 1
    
    Expected output
    7
    
  3. Example 3

    Input
    3 4
    
    Expected output
    5
    
  4. Example 4

    Input
    123 123
    
    Expected output
    0