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