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