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()

local l=n*m//gcd(n,m)
local ln=m//gcd(n,m)
local lm=n//gcd(n,m)

local x={}
for i=1,n do
    x[(i-1)*ln+1]=s:sub(i,i)
end

for i=1,m do
    if not x[(i-1)*lm+1] then
        x[(i-1)*lm+1]=t:sub(i,i)
    elseif x[(i-1)*lm+1]~=t:sub(i,i) then
        print(-1)
        return
    end
end
print(l)