

def gcm(a, b)
	if a > b
		a, b = [b, a]
	end
	mod = b % a
	if mod ==0
		return a
	else
		return gcm(mod, a)
	end
end

def lcm(a, b)
	g = gcm(a, b)
	return g * (a / g) * (b / g)
end

N, M = gets.strip.split(/ /).collect{|n| n.to_i}
S = gets.strip.each_char.collect{|c| c.ord}
T = gets.strip.each_char.collect{|c| c.ord}

def check(cm)
	k1 = cm / N
	k2 = cm / M
	
	l = lcm(k1, k2)
	
	d1 = l / k1
	d2 = l / k2

	cm2 = 0
	while cm2 < cm
		if S[cm2 / k1] != T[cm2 / k2]
			return false
		end
		
		cm2 += l
	end
	
	return true
end

nm = N * M
l = lcm(N, M)
if check(l)
	puts l
else
	puts -1
end
