n, m = gets.chomp.split.map(&:to_i)
s = gets.chomp
t = gets.chomp
l = [n, m].inject(:gcd)
i = 0
j = 0
while i < n && j < m
  puts i
  puts j
  if s[i] != t[j]
    puts -1
    exit
  end
  i += n / l
  j += m / l
end
puts n/l*m