#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
long long X,Y,t;	
long long ans;
const long long inf = 1e18;
long long gcd(long long x,long long y)
{
	return !y ? x : gcd(y,x%y);
}
int main()
{
	cin >> X >> Y;
	if (X<Y) X^=Y,Y^=X,X^=Y;
	t = gcd(X,Y);
	if (t==Y) 
	{
		puts("-1");
		return 0;
	}
	Y /= t;
	if (Y == 2) ans = X * 3;else ans = X * 2;
	if(ans>inf) puts("-1");
	else cout << ans<<endl; 
	return 0;
}