require 'pp'
# IS_DEBUG = true
IS_DEBUG = false
def dputs str
	if(IS_DEBUG)
		puts str
	end
end
def dpp str
	if(IS_DEBUG)
		pp str
	end
end

def gcd(a,b)
	if(a>b)
		return gcd(b,a)
	end
	if(b%a == 0)
		return a
	else
		return gcd(a,b%a)
	end
end
def lcm(a,b)
	a/gcd(a,b)*b
end

MAX_INT = 9999999999999999

N,M=gets.chomp.split(' ').map{|n| n.to_i}
S=gets.chomp.split('').map{|n| n}
T=gets.chomp.split('').map{|n| n}
g = gcd(N,M)
nd = N/g
md = M/g
for i in 0..(g-1)
	if(S[i*nd] != T[i*md])
		puts -1
		exit(0)
	end
end
puts lcm(N,M)
