from math import gcd d, k = map(int, input().split()) print(d * gcd(d ** 333, k) * ((d % 4 == 2 and k % 2 == 0 and k >= 4) + 1))