n,m=gets.split.map &:to_i
s=gets.chop
t=gets.chop
len=n.lcm(m)
ns=[]
for i in 0...n
    ns<<i*len/n
end
for i in 0...m
    j=i*len/m
    if ns.include?(j) && s[j]!=t[j]
        p -1
        exit
    end
end
p len