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
    local p=(i-1)*ln+1
    x[p]=s:sub(i,i)
end

local bool=true
for i=1,m do
    local p=(i-1)*lm+1
    if x[p] and x[p]~=t:sub(i,i) then
        bool=false
        break
    end
end
print(bool and l or -1)