N, M = gets.split.map(&:to_i)
L = N.lcm(M)
S = gets.chomp.chars
T = gets.chomp.chars
S.each_with_index do |s, i|
    if ((L / N) * i % (L / M) == 0 && s != T[(L / N) * i / (L / M)])
        p -1
        exit
    end
end
p L