n,m=gets.split.map &:to_i
s=gets.chop
t=gets.chop
len=n.lcm(m)
x1=Array.new(len,"")
x2=Array.new(len,"")
ln=len/n
for i in 0...n
    x1[i*ln]=s[i]
end
lm=len/m
for i in 0...m
    x2[i*lm]=t[i]
end
p (0...len).all?{|i|x1[i]==x2[i]||x1[i]==""||x2[i]==""} ? len : -1