#include using namespace std; vector enum_divisor(long long n){ vector ret; for (long long i = 1; i * i <= n; ++i){ if (n % i == 0){ ret.push_back(i); if (i * i != n){ ret.push_back(n / i); } } } return ret; } int main() { ios::sync_with_stdio(false); cout.tie(0); long long 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); long long ans = A + B + 1; for (auto c: cs){ if ((B + c) % A == 0 and (c + A) % B == 0 and c < ans and c != A and c != B) ans = c; } if (ans == A + B + 1) ans = -1; cout << ans << endl; return 0; }