n, m = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
if S[0] != T[0]
  puts '-1'
  exit
end

c = n.lcm(m)
i = 1
loop do
  d = c * i
  x = Array.new(d, '*')
  y_n = d / n
  a = [1]
  (1..n-1).each do |j|
    a << j * y_n + 1
  end
  b = [1]
  y_m = d / m
  (1..m-1).each do |j|
    b << j * y_m + 1
  end
  a.each_with_index{|j, k| x[j-1] = S[k]}
  flag = true
  b.each_with_index do |j, k|
    next if x[j-1] == '*' || x[j-1] == T[k]
     flag = false
  end
  if flag
    puts d
    exit
  end

  i += 1
  break if i > 10
end
puts '-1'