n,m=gets.split.map(&:to_i)
s=gets.chomp.split('')
t=gets.chomp.split('')
ans=[n,m].inject(:lcm)
if n%2==0 && m%2==0 then
	puts ans
	exit
end

if n%2==0 || m%2 ==0 then
	j=0
	i=2*j+1
	while i < [n,m].max - 1
		i=2*j+1
		if s[i] == t[i] then
			puts "-1"
			exit
		end
		j+=1
		i=2*j+1
	end
end
puts ans