n, m = gets.split.map(&:to_i)

s = gets
t = gets

len = n.lcm(m)

dn = len / n
dm = len / m
gd = dn.lcm(dm)

(len / gd).times do |k|
    ds = s[(gd / dn) * k]
    dt = t[(gd / dm) * k]
    if dt != ds
        puts "-1"
        return
    end
end

puts len