n, m = gets.split(" ").map(&:to_i)
s = gets.chomp.to_s
t = gets.chomp.to_s

lcm = s.size.lcm(t.size)

x = []

lcm.times do
  x.push("")
end

lcm_s = lcm / s.size

s.size.times do |num|
  x[(1 + lcm_s * num)] = s[num]
end

lcm_t = lcm / t.size

t.size.times do |num|
  if x[(1 + lcm_t * num)] != "" && x[(1 + lcm_t * num)] != t[num]
    puts "-1"
    exit
  end
end

puts lcm