#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b;
    while (cin>>a>>b) {
        long long _gcd=__gcd(a,b);
        b/=_gcd;
        if (b==1) puts("-1");
        else {
            for (long long i=2;;++i) {
                if (__gcd(i,b)!=b) {
                    cout<<i*a<<endl;
                    break;
                }
            }
        }
    }
    return 0;
}
