#include #include int n, d; std::set s; int cur, cnt; int main() { std::cin >> n >> d; if (d == 1)std::cout << n << std::endl; else if (d == n - 1)std::cout << n << std::endl; else { while (s.find(cur) == s.end()) { s.insert(cur); cur = (cur + d) % n; ++cnt; } std::cout << cnt - 1 << std::endl; } return 0; }