N,M = gets.split.map(&:to_i)
S = gets.chomp
T = gets.chomp
L = N*M/N.gcd(M)
h = {}; g = {}
(0...N).each{ |i| h[i*L/N] = i }
(0...M).each{ |i| g[i*L/M] = i }
p h.keys.all?{ |a| !g[a] || S[h[a]] == T[g[a]] } ? L : -1
