N, M = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
L = N.lcm(M)

list = Array.new(L, -1)
list[0] = S[0]

1.upto(N - 1) do |i|
  list[i * L / N] = S[i]
end

if list[0] != T[0]
  puts -1
  exit
end

1.upto(M - 1) do |j|
  idx = j * L / M

  if list[idx] != -1 && list[idx] != T[j]
    puts -1
    exit
  end

  list[idx] = T[j]
end

puts L
