#include using namespace std; typedef long long ll; #define F first #define S second #define pii pair #define eb emplace_back #define all(v) v.begin(), v.end() #define rep(i, n) for (int i = 0; i < n; ++i) #define rep3(i, l, n) for (int i = l; i < n; ++i) #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) #define out(a) cout << a << endl #define SZ(v) (int)v.size() #define inf (int)(1e9+7) // 約数の列挙 O(√n) vector enum_divisor(int n) { vector res; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { res.push_back(i); if (i != n / i) res.push_back(n / i); } } return res; } int main() { int a, b; cin >> a >> b; auto v = enum_divisor(a + b); int ans = 2e9; rep(i, SZ(v)) { if ((v[i] + a) % b != 0 || (v[i] + b) % a != 0) continue; if (v[i] == a || v[i] == b) continue; chmin(ans, v[i]); } if (ans == 1e9) out(-1); else out(ans); }