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

LCM = n.lcm(m)

x = Array.new(LCM*2)

if s[0] != t[0]
	puts "-1"
	exit
end

n_ = LCM / n
i = 1
while n_ * i < LCM
	x[n_*i] = s[i]
	i += 1
end

m_ = LCM / m
i = 1
while m_ * i < LCM
	if(x[m_*i].nil? || x[m_*i] == t[i])
		x[m_*i] = t[i]
	else
		puts "-1"
		exit
	end
	i += 1
end

puts LCM