def gcd(lhs, rhs)
  return lhs if rhs == 0
  return gcd(rhs, lhs) if lhs < rhs

  mod = lhs % rhs
  div = (lhs - mod) / rhs

  return gcd(rhs, mod)
end

s_length, t_length = gets.split.map(&:to_i)
s = gets.chomp
t = gets.chomp

gcd_res = gcd(s_length, t_length)
x_length = s_length * t_length / gcd_res

ok = true
(x_length / gcd_res).times do |i|
  if s[i * s_length / gcd_res] != t[i * t_length / gcd_res]
    ok = false
    break
  end
end

if ok
  puts x_length
else
  puts -1
end
