N, D = gets.split.map(&:to_i) g = N.gcd(D) if g == 1 puts N - 1 else puts N / g - 1 end