implicit none

integer:: n,i,j,k,count,jj,count_max
character,allocatable,dimension(:) :: c*1
integer,allocatable,dimension(:):: x,y
read (5,*) n,k

allocate(x(n))
allocate(y(n))
allocate(c(n))

do i=1,n
    read(5,*) x(i),y(i),c(i)
enddo

do j=1,k
    do jj=1,k
    count=0
    do i=1,n
        if (mod(mod(x(i)-j+1,k),2)==1 .and. mod(mod(y(i)-jj+1,k),2)==0 .and. c(i)=="W" ) then
            count=count+1
        if (mod(mod(x(i)-j+1,k),2)==0 .and. mod(mod(y(i)-jj+1,k),2)==1 .and. c(i)=="W" ) then
            count=count+1
        if (mod(mod(x(i)-j+1,k),2)==0 .and. mod(mod(y(i)-jj+1,k),2)==0 .and. c(i)=="B" ) then
            count=count+1
        if (mod(mod(x(i)-j+1,k),2)==1 .and. mod(mod(y(i)-jj+1,k),2)==1 .and. c(i)=="B" ) then
            count=count+1
        endif
        endif
        endif
        endif
enddo
enddo
        count_max=max(count,n-count)
enddo

!write(*,*) n,k
!write(*,*) x,y,c
write(*,*) count_max

stop
end
