input_line = input().split() a = int(input_line[0]) b = int(input_line[1]) max_n = 0 if(a > b): big_n = a small_n = b else: big_n = b small_n = a def check_c(c): if(((a+b) % c) == 0 and (b+c) % a == 0 and (c+a) % b == 0): return True else: return False c = big_n - small_n while(c <= big_n+small_n): if(c == big_n or c == small_n): continue elif(check_c(c)): print(c) exit() else: pass c += big_n print(-1)