n, m = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
c = n.lcm(m)
y_n = c / n
y_m = c / m
a = [0]
(1..n - 1).each { |j| a << j * y_n }
b = [0]
(1..m - 1).each { |j| b << j * y_m }
(a & b).each do |j|
  a_i = a.index(j)
  b_i = b.index(j)
  if S[a_i] != T[b_i]
    c = -1
    break
  end
end
puts c
