N, M = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp

def test(n, m)
  i = j = 0
  while i < N && j < M
    #p [i, j, S[i], T[j]]
    return false if S[i] != T[j]
    i += n
    j += m
  end
  return true
end

lcm = N.lcm(M)
ok = if S[0] != T[0]
       false
     elsif lcm == N
       test(lcm/M, 1)
     elsif lcm == M
       test(1, lcm/N)
     else
       true
     end
p(ok ? lcm : -1)