n, m = gets.chomp.split.map(&:to_i)
s = gets.chomp
t = gets.chomp
if s[0] != t[0]
  puts -1
  exit
end
l = [n, m].inject(:lcm)
loop do
  if l > 100000
    puts -1
    exit 
  end
  a1 = Array.new(l / n - 1, ' ').join
  a2 = Array.new(l / m - 1, ' ').join
  x1 = s.split("").join(a1)
  x2 = t.split("").join(a2)
  if l % n != 0 || l % m != 0
    l += 1
    next
  end
  flag = true;
  [x1.length, x2.length].min.times do |i|
    next if i == 0
    next if x1[i] == ' '
    next if x2[i] == ' '
    flag = false if x1[i] != x2[i]
    break if x1[i] != x2[i]
    next if x1[i] == x2[i]
  end
  if flag
    puts l
    exit
  end
  l *= 2
end
