#include <iostream>

bool judge(int A, int B, int C) {
    if ((A + B) % C != 0) return false;
    if ((B + C) % A != 0) return false;
    if ((C + A) % B != 0) return false;
    return true;
}

void answer(int C) {
    std::cout << C << std::endl;
    std::exit(0);
}

int main() {
    int A, B;
    std::cin >> A >> B;

    for (int C = 1; C * C <= A + B; ++C) {
        if (judge(A, B, C)) answer(C);
        if (judge(A, B, (A + B) / C)) answer((A + B) / C);
    }

    std::cout << -1 << std::endl;
    return 0;
}