local function gcd(a,b)
    if b>0 then
        return gcd(b,a%b)
    else
        return a
    end
end

local n,m=io.read("*n","*n","*l")
local s=io.read()
local t=io.read()

if n<m then
    n,m=m,n
end

if n%m==0 then
    print(-1)
else
    print(n*m//gcd(n,m))
end