#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 998244353; constexpr bool debug = false; //---------------------------------// namespace tk { template T ext_gcd(const T & a, T & x, const T & b, T & y) { assert(a > 0); assert(b > 0); T a0 = a, a1 = 1, a2 = 0, b0 = b, b1 = 0, b2 = 1; while (b0 > 0) { T q = a0 / b0, r = a0 % b0; T nb1 = a1 - q * b1, nb2 = a2 - q * b2; a0 = b0; b0 = r; a1 = b1; b1 = nb1; a2 = b2; b2 = nb2; } x = a1; y = a2; return a0; } } // namespace tk int main() { int A, B; cin >> A >> B; int x, y; int g = tk::ext_gcd(A, x, B, y); if (g != 1) puts("-1"); else { if (x >= 0) { swap(A, B); swap(x, y); } int ans = 0; const int ab = A * B; int basel = -A * x, baser = B * y; int l = basel, r = baser; for (int k = 1; r - l < ab; ++k, l += basel, r += baser) { int d = r / ab * ab; if (d < l) ++ans; } cout << ans << endl; } }