#include using namespace std; typedef long long ll; typedef vector vl; typedef long double ld; typedef vector vd; typedef bool bl; typedef vector vb; typedef unordered_map> graph; const ll e5 = 1 << 20; const ll mod = 998244353; const ll e3 = 1 << 13; const ll INF = 1ll << 60; ll a,b; ll c; ll ans = INF; int main(){ cin >> a >> b; for(c = 1;c*c <= a+b;c++){ if((a+b)%c == 0){ if((a+c)%b == 0 && (b+c)%a == 0) ans = min(ans,c); ll c_ = (a+b)/c; if((a+c_)%b == 0 && (b+c_)%a == 0) ans = min(ans,c_); } } cout << (ans == INF ? -1 : ans) << endl; }