#include #define rep(i, n) for(long long i = 0; i < (long long)(n); i++) using namespace std; using ll = long long; const ll INF = 1e18; // 約数列挙 vector calc_divisor(long long n) { vector res; for (long long i = 1LL; i*i <= n; ++i) { if (n % i == 0) { res.push_back(i); long long j = n / i; if (j != i) res.push_back(j); } } sort(res.begin(), res.end()); //sort(res.rbegin(), res.rend()); return res; } int main(){ ll a, b; cin >> a >> b; auto d = calc_divisor(a+b); ll ans = INF; rep(i, d.size()){ ll c = (a + b) / d[i]; if((a + c) % b == 0 && (b + c) % a == 0) ans = min(ans, c); } if(ans == INF) cout << "-1" << endl; else cout << ans << endl; return 0; }