#include<cstdio>
#include<cstring>
const int MAXN = 100000;
const int MAXK = 1000;
int x[MAXN + 5], y[MAXN + 5], c[MAXN + 5];
int y1[10 * MAXK + 5];
int main() {
	int N, K, Max = 0, cnt = 0;
	scanf("%d%d",&N,&K);
	for(int i=1;i<=N;i++) {
		char ch;
		scanf("%d%d %c",&x[i],&y[i],&ch);
		x[i] = x[i] % (2*K);
		y[i] = y[i] % (2*K);
		if( ch == 'W' ) c[i] = 1;
		else c[i] = 0;
	}
	for(int i=0;i<=K;i++) {
		cnt = 0;
		memset(y1,0,sizeof(y1));
		for(int j=1;j<=N;j++) {
			int p = ( i <= x[j] && x[j] < i+K );
			int q = ( 0 <= y[j] && y[j] < K );
			if( p ^ q == c[j] ){
				cnt++;
				y1[y[j]] ++;
			}
			else {
				y1[y[j]] --;
			}
		}
		for(int j=0;j<=K;j++) {
			if( cnt > Max ) Max = cnt;
			if( N - cnt > Max ) Max = N - cnt;
			if( j != K )
				cnt = cnt - y1[j] - y1[K+j];
		}
	}
	printf("%d\n",Max);
}
                                      
                                          
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                           
                                            
                                             
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                              
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                 
                                                  

Measured memory_bytes: 1408