import math A, B = map(int, raw_input().split()) divisors = [] for i in range(1, int(math.sqrt(A+B))+1): if (A+B)%i == 0: divisors.append(i) N = len(divisors) for i in range(0, N): if (A+B)/divisors[i] not in divisors: divisors.append((A+B)/divisors[i]) divisors.sort() min = -1 for i in range(0, len(divisors)): if A != divisors[i] and B != divisors[i]: if (B+divisors[i])%A == 0 and (A+divisors[i])%B == 0: min = divisors[i] break print min