n, b = read_line.split.map(&.to_u64) if b == 1 puts 0 exit end if n == 0 || n.gcd(b) != 1 puts "NaN" exit end n %= b 1.upto(b) do |i| if n * i % b == 1 puts i exit end end