A,B = map(int,input().split()) x = 1 while True: if x % A == B % x: print(x) break x += 1