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