import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long a = sc.nextLong(); long b = sc.nextLong(); long ans = (long)Math.pow(10, 18) + 1; if(a > b) { // 2x, x if(a == (2 * b)) ans = Math.min(ans, 3 * b); // 3x, x if(a == (3 * b)) ans = Math.min(ans, 2 * b); // 3x, 2x if((a % 3 == 0) && (b % 2 == 0) && ((a / 3) == (b / 2))) ans = Math.min(ans, b / 2); } else { // x, 2x if(b == (2 * a)) ans = Math.min(ans, 3 * a); // x, 3x if(b == (3 * a)) ans = Math.min(ans, 2 * a); // 2x, 3x if((b % 3 == 0) && (a % 2 == 0) && ((b / 3) == (a / 2))) ans = Math.min(ans, a / 2); } if(ans == ((long)Math.pow(10, 18) + 1)) ans = -1; System.out.println(ans); } }