#include <bits/stdc++.h>
#define F first
#define S second
#define dq deque
#define vc vector
#define INF (0x3f3f3f3f)
#define LINF ((long long)(0x3f3f3f3f))
#define Pb push_back
#define Mp make_pair
#define ll long long
#define _tp template
#define _tyn typename
#define Pf push_front
#define sint short int
#define ull unsigned ll
#define pii pair<int,int>
#define uint unsigned int
#define B cout << "BreakPoint" << endl;
#define Ms(_data) memset(_data,0,sizeof(_data))
#define Fin(_filename) freopen(_filename,"r",stdin)
#define Fout(_filename) freopen(_filename,"w",stdout)
#define Msn(_data,_num) memset(_data,_num,sizeof(_data))
#define Fastio ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
#define Mymax(a,b) if(a<b) a = b;
#define Mymin(a,b) if(a>b) a = b;
void Print(int _x){printf("%d\n",_x);}
void Print(ll _x){printf("%lld\n",_x);}
_tp<_tyn T>void Print( T _a[] , int _s , int _t ){
	for( int i = _s ; i <= _t ; i++ )
		cout << _a[i] << " ";
   	cout << endl;
}
#define il inline
#define rg register
struct InputReader{
    static const int bs = 100000;
    char buf[bs]; int p;
    il InputReader():p(bs){}
    il void Flush(){
        p = 0;
        fread(buf,1,bs,stdin);
    }
    il char C(){
        if(p >= bs) Flush();
        return buf[p++];
    }
	il int Getnum(){
		char ch = C();
        while( ch < '0'  ||  ch > '9' ) ch = C();
		return (int)(ch-'0');
	}
    il int operator() (){
        int ans = 0;
        char ch = C();
		int fu = 1;
		while( ch < '0'  ||  ch > '9' ){
			if( ch == '-' ) fu = -1;
			ch = C();
		}
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10;
            ans += ch-'0';
            ch = C();
        }
        return ans * fu;
    }
	il ll Readll(){
		ll ans = 0LL;
		char ch = Getnum()+'0';
        while( ch >= '0'  &&  ch <= '9' ){
            ans *= 10LL;
            ans += ch-'0';
            ch = C();
        }
        return ans;
	}
	il int Specialread(){
		char ch = C();
		while( ch != 'W'  &&  ch != 'B' ) ch = C();
		return ch;
	}
}In;
il void Read( int &x ){ x = In(); }
il void Read( int &x, int &y ){ x = In(); y = In(); }
il void Read( int &x1 , int &x2 , int &x3 ){
	x1 = In(); x2 = In(); x3 = In();
}
il void Read( int &x1 , int &x2 , int &x3 , int &x4 ){
	x1 = In(); x2 = In(); x3 = In(); x4 = In();
}
il void Read( ll &x ){
	x = In.Readll();
}
clock_t _starttime,_endtime;
il void FILEIO(){
	#ifdef WIN32
		_starttime = clock();
		Fin("d.in");
	#endif
}
#ifdef WIN32
	#include <windows.h>
	#include <psapi.h>
	#pragma comment(lib,"Psapi.lib")
#endif
il void END(){
	#ifdef WIN32
		_endtime = clock();
		double _timeuse = (_endtime-_starttime)* 1000.0 / CLOCKS_PER_SEC;
		printf("\n\nTime usage:\n%.0lf ms\n",_timeuse);
		HANDLE _handle = GetCurrentProcess();
		PROCESS_MEMORY_COUNTERS pmc;
		GetProcessMemoryInfo(_handle,&pmc,sizeof(pmc));
		double _memuse = pmc.PeakPagefileUsage/1024.00/1024.00;
		printf("Memory usage:\n%.1lf MB\n\n",_memuse);
	#endif
	exit(0);
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
#define MAXN 100010
#define MAXK 2010

int n,k;
int sum[MAXK][MAXK];
int cnt[MAXK][MAXK];

int Getsum( int x1 , int y1 , int x2 , int y2 ){
	if( x2 < 0  ||  y2 < 0 ) return 0;
	if( x1 > 2*k-1  ||  y1 > 2*k-1 ) return 0;
	Mymax(x1,0);
	Mymax(y1,0);
	Mymin(x2,2*k-1);
	Mymin(y2,2*k-1);
	return sum[x2][y2] - sum[x1-1][y2] - sum[x2][y1-1] + sum[x1-1][y1-1];
}

int main(){
	FILEIO();
	Ms(cnt);

	Read(n,k);
	for( int i = 1 ; i <= n ; i++ ){
		int x,y,col;
		Read(x,y);
		char c;
		c = In.Specialread();
		if( c == 'B' ) col = 1;
		else col = 0;
		if( col == 0 ){
			x += k;
		}
		x %= 2*k;
		y %= 2*k;
		cnt[x][y]++;
		//printf("%d %d %d\n",x[i],y[i],color[i]);
	}

	Ms(sum);
	sum[0][0] = cnt[0][0];
	for( int i = 1 ; i <= 2*k ; i++ ){
		sum[i][0] = sum[i-1][0] + cnt[i][0];
		sum[0][i] = sum[0][i-1] + cnt[0][i];
	}
	for( int i = 1 ; i <= 2*k ; i++ )
		for( int j = 1 ; j <= 2*k ; j++ )
			sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + cnt[i][j];

	int ans = 0;
	for( int i = 0 ; i >= -(2*k+10) ; i-- )
		for( int j = 0 ; j >= -(2*k+10) ; j-- ){
			int cnt = 0;
			for( int x = i , a = 1 ; x < 2*k ; x += k , a++ )
				for( int y = j , b = 1 ; y < 2*k ; y += k , b++ ){
					if( abs(a-b) % 2 != 0 )
						cnt += Getsum(x,y,x+k-1,y+k-1);
				}
			Mymax( ans , cnt );
		}

	printf("%d\n",ans);

	END();
}


Measured memory_bytes: 31872