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