#最大公約数 def gcd(a,b): while b!=0: a,b=b,a%b return a #最小公倍数 def lcm(a,b): return a*b//gcd(a,b) A,B=map(int,input().split()) if A==1 or B==1: print(0) exit() Al=[] for i in range(1,int(A**0.5)+1): if A/i==A//i: Al.append(i) Al.append(A//i) #print(Al) for item in Al[1:]: if B%item==0: print(-1) exit() eu=(lcm(A,B)) ans=0 for num in range(1,eu): org=num while num>-1: if num%B==0: ans+=1 break else: num-=A print(eu-ans-1)