def gcd(a,b): if a>b: m=a t=b else: m=b t=a r=m%t while r!=0: m=t t=r r=m%t return t N,D=(int(i)for i in input (). split ()) N//=gcd(N,D) print (N-1)