#include using namespace std; using ll = long long; constexpr char newl = '\n'; // a * x + b * y == gcd(a, b) なるx, yを計算 // 返り値はgcd(a, b) template T extgcd(T a, T b, T& x, T& y) { T d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int a, b; cin >> a >> b; if (__gcd(a, b) != 1) { cout << -1 << newl; return 0; } int x, y; extgcd(a, b, x, y); int cnt = 0; for (int i = 1; i <= 100000; i++) { int L = ((-x) * i + b - 1) / b; int R = y * i / a; if (L > R) ++cnt; } cout << cnt << newl; return 0; }