N, M = gets.split.map(&:to_i)

S = gets.chomp
T = gets.chomp
Nnum, Mnum,  nmnum = [],[],[]

class Array
  def lcm
    self.inject{|a,b| a.lcm(b)}
  end

  def gcd
    self.inject{|a,b| a.gcd(b)}
  end
end

inc = [N,M].lcm

i = inc
while i <= 100001 do
	N.times do |j|
		Nnum.push(j*(i/N)+1)
	end
	M.times do |k|
		Mnum.push(k*(i/M)+1)
	end	
	#p Nnum, Mnum
	nmnum = Nnum & Mnum
	#番号が1のみ
	
	if nmnum == [1]
		if S[0] == T[0]
			print("#{i}\n")
			exit
		else
			print("-1\n")
			exit
		end
	end
	
	for j in nmnum
		n = ((j-1)*N)/i
		m = ((j-1)*M)/i
		if S[n] != T[m]
			break
		end
		if j == nmnum[-1]
			print("#{i}\n")
			exit
		end
	end
	
	i += inc
	Nnum.clear
	Mnum.clear
	nmnum.clear
end
print("-1\n")



