program modulo_matrix
  implicit none
  integer :: x, y
  character(100000) :: s, t
  integer(8) :: n, m, i
  read(*,*) n, m
  read(*,*) s
  read(*,*) t
  do i = 0_8, n-1_8
    if (mod(i*m,n) /= 0_8) cycle
    x = int(i,4)+1
    y = int(i*m/n,4)+1
    if (s(x:x) /= t(y:y)) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  write(*,'(i0)') lcm(n,m)
contains
  integer(8) function lcm(a,b)
    integer(8), intent(in) :: a, b
    lcm = abs(a)/gcd(a,b)*abs(b)
  end
  integer(8) function gcd(a,b)
    integer(8), intent(in) :: a, b
    integer(8) :: x, y
    x = max(abs(a),abs(b))
    y = min(abs(a),abs(b))
    do while (y > 0_8)
      gcd = y
      y = mod(x,y)
      x = gcd
    end do
  end
end program modulo_matrix