A,B = [int(v) for v in input().split(" ")] x = 1 while True: if x % A == B % x: print(x) break x += 1