n, d = list(map(int, input().split(' ')))

if n == d:
    print(0)
else:
    import math
    print((n // math.gcd(n, d)) - 1)