DBG = !true

n,m = gets.split.map{|x| x.to_i}
s = gets.chomp!
t = gets.chomp!
printf("n:%d m:%d s:%s t:%s\n", n, m, s, t) if DBG

g = n.gcd(m)
l = n.lcm(m)
ok = true
for i in 0...g
  if s[i*n/g] != t[i*m/g]
    ok = false
    break
  end
end
print (ok ? l : -1)
