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