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