def gcd(a,b): if a % b == 0: return b return gcd(b, a%b) n,d = map(int, raw_input().split()) print n / gcd(n,d) - 1