def gcd(a,b)
    return a if b == 0
    gcd(b,a%b)
end

def lcm(a,b)
    a * b / gcd(a,b)
end

n,m=gets.chomp.split.map(&:to_i)
s=gets.chomp.split("")
t=gets.chomp.split("")

ss={}
tt={}
l = lcm(n,m)
0.upto(s.size-1) do |i|
    ss[i*l/s.size]  = i
end
0.upto(t.size-1) do |i|
    tt[i*l/t.size] = i
end


ss.each do |k, v|
    if tt.has_key?(k)
        if s[ss[k]] != t[tt[k]]
            puts -1
            exit
        end
    end
end
puts l
