#include using namespace std; vector enum_divisor(int n){ vector ret; for (int i = 1; i * i <= n; ++i){ if (n % i == 0){ ret.push_back(i); if (i != 1 and i * i != n){ ret.push_back(n / i); } } } return ret; } int main() { ios::sync_with_stdio(false); cout.tie(0); int A, B; cin >> A >> B; // a + b = k1 * c // b + c = k2 * a -> (k1*c-a)+c=k2*a ->(k1+1)*c = (k2+1)*a // c + a = k3 * b -> (c + k1*c-b) = k3*b => (k1+1)*c = (k3+1)*b // (k2+1)*a = (k3+1)*b auto cs = enum_divisor(A + B); int ans = A + B + 1; for (auto c: cs){ if ((B + c) % A == 0 and (c + A) % B == 0 and c < ans) ans = c; } if (ans == A + B + 1) ans = -1; cout << ans << endl; return 0; }