#include using namespace std; using ll = long long; using ld = long double; #define FOR(i, m, n) for (int i = (m); i < (n); i++) #define FORR(i, m, n) for (int i = (m); i >= (n); i--) #define REP(i, n) FOR(i, 0, (n)) #define REPR(i, n) FORR(i, (n) - 1, 0) #define REP1(i, n) FOR(i, 1, (n) + 1) #define REPS(c, s) for (char c : s) #define ALL(c) (c).begin(), (c).end() #define SORT(c) sort(ALL(c)) #define REV(c) reverse(ALL(c)) #define sz(v) (int)v.size() #define endl '\n' template inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;} template inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;} const int MOD = 1000000007; const int INF = 1000000001; const ll LINF = 1000000001000000001LL; void solve(); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(numeric_limits::max_digits10); solve(); return 0; } void solve() { int a, b; cin >> a >> b; if (a % 2 == 0 && b % 2 == 0) { cout << -1 << endl; return; } if (a > b) swap(a, b); bool ok = true; REP1(i, b - 1) { bool k = false; REP1(j, b) { if ((a * j) % b == i) { k = true; break; } } if (!k) { ok = false; break; } } if (!ok) { cout << - 1 << endl; return; } set s; REP(i, 2000) REP(j, 2000) { s.emplace(a * i + b * j); } int ans = 0; REP1(i, *(prev(s.end())) * 2 / 3) { if (s.count(i) == 0) ans++; } // cout << a * 999 + b * 999 << endl; // cout << *(prev(s.end())) << endl; cout << ans << endl; }