#include #include #include #include using namespace std; using ll = long long; template using grid = vector>; #define REP(i,n) for(ll i=0;i<(ll)(n);i++) #define REPD(i,n) for(ll i=n-1;i>=0;i--) #define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++) #define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--) #define input(...) __VA_ARGS__; in(__VA_ARGS__) void print() { std::cout << std::endl; } template void print(Head&& head, Tail&&... tail) { std::cout << head << " "; print(std::forward(tail)...); } void in() { } template void in(Head&& head, Tail&&... tail) { cin >> head; in(std::forward(tail)...); } int main() { ll input(A, B); if (A == 1 || B == 1) { print(0); return 0; } /* if (A % B == 0 || B % A == 0) { */ /* print(-1); */ /* return 0; */ /* } */ vector result; REP(x, 3000) { REP(y, 3000) { result.push_back(A * x + B * y); } } sort(result.begin(), result.end()); ll p = 0; ll check = 0; int ans = 0; for(auto v: result) { int range = max((v - p) - 1, 0ll); if (range > 0) { if (v - check > 1000) { print(ans); return 0; } check = v; } ans += range; p = v; } print(-1); }