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

l = n*m/(n.gcd(m))

i = 0
flag = true
while i*l*l/(n*m) < l
  if !(s[i*l/m] == nil or s[i*l/m] == t[i*l/n])
    flag = false
    break
  end
    i += 1
end


if flag
  puts(l)
else
  puts("-1")
end
