#include #define int long long #define double long double #define INF 1e18 using namespace std; signed main() { int A, B; cin >> A >> B; int ans = ((2*A+B)%B == 0 && (A+2*B)%A == 0) ? A+B : -1; for (int K = 2; K*K <= A+B; K++) { if ((A+B)%K == 0) { int C = (A+B)/K; if ((A+C)%B == 0 && (B+C)%A == 0 && C != A && C != B) { ans = C; } } } cout << ans << endl; }