a,b=map(int,input().split()) t=[] for i in range(1,a): x=1 while x: if x%a==b%x: t.append(x) x=0 x+=1 print(min(t))