

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
T = gets.strip

def check(cm)
	table = []
	k = cm / N
	N.times{|j|
		table[k * j] = S[j]
	}
	k = cm / M
	M.times{|j|
		c1 = T[j]
		c2 = table[k * j]
		if c2 != nil && c1 != c2
			return false
		end
	}
	return true
end


nm = N * M
l = lcm(N, M)
i = 1
while true
	cm = l * i
	if cm > nm
		puts -1
		break
	end
	if check(cm)
		puts cm
		break
	end
	
	i += 1
end

