program main
Implicit None
character::s*100000,t*100000
integer(8)::n,m,x,y,p,q,i,k,a,b
	
	read *,n,m
	read *,s
	read *,t
	
	
	
	call lcm(n,m,y)
	
	p = y/m
	q = y/n
	k = 0
	
	do i = 1,n*m/y
		a = (i-1)*p + 1
		b = (i-1)*q + 1
		
		if(s(a:a) /= t(b:b))then
			k = 1
			exit
		endif
	enddo
	
	if (k == 0)then
		print 102,y
		else
		print 101,"-1"
	endif


101 format(a)
102 format(i0)
end program main


subroutine euc(a1,b1,c)
implicit None
integer::a1,b1,c,a,b
	
	a = a1
	b = b1
	c = 1
	
	do while (a /=0 .and. b /= 0)
		if (a > b)then
			c = b
			a = mod(a,b)
			else
			c = a
			b = mod(b,a)
		endif
	enddo
end subroutine euc

subroutine lcm(a1,b1,c)
implicit none
integer::a1,b1,c
	call euc(a1,b1,c)
	c = a1 * b1 /c

end subroutine lcm
