import math n,d = map(int,input().split()) g = math.gcd(n,d) nn = n*(d//g) print(nn//d-1)