INF = 10 ** 9 MOD = 10 ** 9 + 7 import sys sys.setrecursionlimit(100000000) dy = (-1,0,1,0) dx = (0,1,0,-1) from copy import deepcopy from bisect import bisect_left def main(): a,b = map(int,input().split()) n = a + b ans = -1 for i in range(1,n): if i * i > n: break if n%i == 0: c = i if c != a and c != b and (b + c)%a == 0 and (c + a)%b == 0: ans = c break c = n//i if c != a and c != b and (b + c)%a == 0 and (c + a)%b == 0: ans = c break print(ans) if __name__ == '__main__': main()