def divisor_list_calc(n): divisor_list = [] for i in range(1, int(n ** 0.5) + 1): if n % i == 0: divisor_list.append(i) if n // i != i: divisor_list.append(n) return divisor_list a, b = map(int,input().split()) c_list = divisor_list_calc(a + b) res_list = [] for i in range(len(c_list)): cond_list = [] cond_list.append((b + c_list[i]) % a == 0) cond_list.append((a + c_list[i]) % b == 0) cond_list.append(a != c_list[i]) cond_list.append(b != c_list[i]) if all(cond_list): res_list.append(c_list[i]) if len(res_list) == 0: res = -1 else: res = min(res_list) print(res)