a,b=map(str,input().split()) c=[] for x in range(1,10**5+1): if x%a==b%x: c.append(x) print(c[0])