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

a = k / n
b = k / m
i = 0
j = 0
ok = true
while i < a.size and j < b.size
  if s[i] != t[j]
    ok = false
    break
  end
  i += b
  j += a
end
if ok
  p k
else
  p -1
end
