    import scala.io.StdIn._
     
    object Main {
        def main(args: Array[String]): Unit = {
            val Array(x, y) = readLine.split(" ").map(_.toInt)
            if (y % x == 0) {
                println(-1)
            } else {
                println(f(x, y))
            }
        }
        
        def f(x: Int, y: Int): Int = {
            if (x > 1.0E18 ) {
                -1
            } else if (x * y == 0) {
                f(x + x, y)
            } else {
                x
            }
        }
    }