#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int N,K;

int board[2005][2005];

int getBlackNum(int x1,int y1,int x2,int y2)
{
	return board[x2][y2] - board[x1][y2] - board[x2][y1] + board[x1][y1];
}

int main()
{
	scanf("%d %d",&N,&K);
	char ch;
	int x,y;
	for(int i=0 ; i<N ; i++)
	{
		scanf("%d %d %c",&x,&y,&ch);
		if(ch == 'W')x += K;
		x %= K*2;
		y %= K*2;
		board[x+1][y+1]++;
	}
	for(int i=1 ; i<=2*K ; i++)
	{
		for(int j=1 ; j<=2*K ; j++)
		{
			board[i][j] += board[i-1][j] + board[i][j-1] - board[i-1][j-1];
		}
	}
	int Max = 0;
	for(int i=1; i<=K ; i++)
	{
		for(int j=1 ; j<=K ; j++)
		{
			int mid = getBlackNum(0,0,i,j) + getBlackNum(0,j+K,i,2*K) 
					+ getBlackNum(i+K,0,2*K,j) + getBlackNum(i+K,j+K,2*K,2*K)
					+ getBlackNum(i,j,i+K,j+K);
			Max = max(Max,max(mid,N-mid));
		}
	}
	printf("%d\n",Max);
	return 0;
}