#include #define rep(i,n) for(int i=(0);i<(n);i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll a, b; cin >> a >> b; ll y = a + b; vector ans; for(ll x = 1; x * x <= y; x++){ if(y % x != 0) continue; ll c = x; if((c + a) % b == 0 && (b + c) % a == 0) ans.push_back(c); c = y / x; if((c + a) % b == 0 && (b + c) % a == 0) ans.push_back(c); } if(ans.size() == 0){ cout << -1 << endl; return 0; } sort(ans.begin(), ans.end()); cout << ans[0] << "\n"; }