#include <bits/stdc++.h>
using namespace std;


int x, y;
int main() {
	scanf("%d%d", &x, &y);
	int g = __gcd(x, y);
	x /= g;
	y /= g;
	if (x % y == 0) {
		printf("-1\n");
	} else {
		if (y == 2) {
			printf("%lld\n", 3ll * x * g);
		} else {
			printf("%lld\n", 2ll * x * g);
		}
	}
	
	
	return 0;
}