program checker
  implicit none
  integer :: n, k, x, y, m, t
  integer :: i, j, g(0:4000,0:4000)
  character(1) :: c
  g = 0
  read(*,*) n, k
  do i = 1, n
    read(*,*) x, y, c
    if (c.eq."W") y = y + k
    x = mod(x,2*k)+1
    y = mod(y,2*k)+1
    g(x,y) = g(x,y) + 1
    g(x,y+2*k) = g(x,y+2*k) + 1
    g(x+2*k,y) = g(x+2*k,y) + 1
    g(x+2*k,y+2*k) = g(x+2*k,y+2*k) + 1
  end do
  do i = 1, 4*k
    do j = 1, 4*k
      g(i,j) = g(i,j) + g(i,j-1)
    end do
  end do
  do j = 1, 4*k
    do i = 1, 4*k
      g(i,j) = g(i,j) + g(i-1,j)
    end do
  end do
  m = 0
  do i = 0, 2*k
    do j = 0, 2*k
      t = g(i+2*k,j+2*k) + 2*g(i+k,j+k) + g(i,j) &
      & - (g(i+2*k,j+k) + g(i+k,j+2*k) + g(i+k,j) + g(i,j+k))
      if (m.lt.t) m = t
    end do
  end do
  write(*,'(i0)') m
  stop
end program checker