import math N,D = map(int,input().split()) gcd = math.gcd(N,D) ii = N//gcd-1 print(ii)