A, B = map(int, input().split()) INF = 10**18 res = INF n = 0 while n ** 2 <= A + B: n += 1 if (A + B) % n == 0: if (A + n) % B == 0 and (B + n) % A == 0: res = min(res, n) if (A + (A + B) // n) % B == 0 and (B + (A + B) // n) % A == 0: res = min(res, (A + B) // n) print(res if res != INF else -1)