#pragma comment(linker, "/STACK:102400000,102400000")
#include <fstream>
#include <iostream>
#include <functional>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <ctime>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <cassert>

//#include <unordered_map>
//#include <unordered_set>
using namespace std;

typedef long long ll;
#define eps 1e-8
#define LL_INF 0x3fffffffffffffff
#define INF 1LL<<30
#define mem(a, b) memset(a, b, sizeof(a))
#define pper(i,n,m) for(int i = n;i >= m; i--)
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))
#define slla(n) scanf("%lld", &(n))
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define DE(val) cout<<#val<<": "<<val<<endl;

const int dx4[5] = { 0,-1,0,1,0 };
const int dy4[5] = { 0,0,1,0,-1 };

const int dx8[9] = { 0,-1,0,1,0,1,1,-1,-1 };
const int dy8[9] = { 0,0,1,0,-1,1,-1,1,-1 };
const int maxn = 5e3 + 50;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
ll po(ll a, ll b, ll mod) { ll res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1)res = res*a%mod; a = a*a%mod; }return res; }
ll gcd(ll a, ll b) { if (a == 0) { return b; } else { return gcd(b%a, a); } }
void YES() { puts("YES"); exit(0); }
void Yes() { puts("Yes"); exit(0); }
void NO() { puts("NO"); exit(0); }
void No() { puts("No"); exit(0); }
void one() { puts("-1"); exit(0); }

int n, k;
int x[233333], y[233333];
int w_s[maxn][maxn];
int b_s[maxn][maxn];
char op[5];

int cal_w(int i, int j)
{
	int ans = 0;
	if (j)ans = w_s[i + k - 1][j - 1];
	if (i&&j)ans -= w_s[i - 1][j - 1];

	if (i)ans += w_s[i - 1][j + k - 1];
	if (i&&j)ans -= w_s[i - 1][j - 1];

	ans += w_s[n - 1][j + k - 1];
	if (j)ans -= w_s[n - 1][j - 1];
	ans -= w_s[i + k - 1][j + k - 1];
	if (j) ans +=w_s[i + k - 1][j - 1];

	ans += w_s[i + k - 1][n - 1];
	ans -= w_s[i + k - 1][j + k - 1];
	if (i)ans -= (w_s[i - 1][n - 1] - w_s[i - 1][j + k - 1]);

	return ans;
}

int cal_b(int i, int j)//i==1 j==2
{
	int ans = 0;
	if (i&&j)
		ans += b_s[i - 1][j - 1];
	if (j)
	{
		ans += b_s[n - 1][j - 1];
		ans -= b_s[i + k - 1][j - 1];
	}

	ans += b_s[i + k - 1][j + k - 1];
	if (j)ans -= b_s[i + k - 1][j - 1];
	if (i)ans -= b_s[i - 1][j + k - 1];
	if( i&&j ) ans += b_s[i - 1][j - 1];

	if (i)ans += b_s[i - 1][n - 1];
	if (i)ans -= b_s[i - 1][j + k - 1];

	ans += b_s[n - 1][n - 1];
	ans -= b_s[n - 1][j + k - 1];
	ans -= (b_s[i + k - 1][n - 1] - b_s[i + k - 1][j + k - 1]);

	return ans;
}

int cal_b2(int i, int j)
{
	int ans = 0;
	if (j)ans = b_s[i + k - 1][j - 1];
	if (i&&j)ans -= b_s[i - 1][j - 1];

	if(i)ans += b_s[i - 1][j + k - 1];
	if (i&&j)ans -= b_s[i - 1][j - 1];

	ans += b_s[n - 1][j + k - 1];
	if (j)ans -= b_s[n - 1][j - 1];
	ans -= b_s[i + k - 1][j + k - 1];
	if (j) ans += b_s[i + k - 1][j - 1];

	ans += b_s[i + k - 1][n - 1];
	ans -= b_s[i + k - 1][j + k - 1];
	if (i)ans -= (b_s[i - 1][n - 1] - b_s[i - 1][j + k - 1]);

	return ans;
}

int cal_w2(int i, int j)//i==1 j==2
{
	int ans = 0;
	if (i&&j)
		ans += w_s[i - 1][j - 1];
	if (j)
	{
		ans += w_s[n - 1][j - 1];
		ans -= w_s[i + k - 1][j - 1];
	}

	ans += w_s[i + k - 1][j + k - 1];
	if (j)ans -= w_s[i + k - 1][j - 1];
	if (i)ans -= w_s[i - 1][j + k - 1];
	if (i&&j) ans += w_s[i - 1][j - 1];

	if (i)ans += w_s[i - 1][n - 1];
	if (i)ans -= w_s[i - 1][j + k - 1];

	ans += w_s[n - 1][n - 1];
	ans -= w_s[n - 1][j + k - 1];
	ans -= (w_s[i + k - 1][n - 1] - w_s[i + k - 1][j + k - 1]);

	return ans;
}

void solve()
{
	sa(n), sa(k);
	repp(i, 1, n)
	{
		sa(x[i]);
		sa(y[i]);
		scanf("%s", op);
		x[i] %= (2 * k);
		y[i] %= (2 * k);
		if (op[0] == 'B')
		{
			b_s[x[i]][y[i]]++;
		}
		else
		{
			w_s[x[i]][y[i]]++;
		}
	}
	n = 2 * k;
	repp(i, 0, 2*k)
	{
		repp(j, 0, 2*k)
		{
			if (j)b_s[i][j] += b_s[i][j - 1];
			if (j)w_s[i][j] += w_s[i][j - 1];
		}
	}

	repp(j, 0, 2*k)
	{
		repp(i, 0, 2*k)
		{
			if(i)b_s[i][j] += b_s[i - 1][j];
			if(i)w_s[i][j] += w_s[i - 1][j];
		}
	}

	int ans = 0;
	rep(i, 0, k)
	{
		rep(j, 0, k)
		{
			if (i == 1 && j == 2)
			{
				i++;
				i--;
			}
			int cnt = cal_w(i, j);
			cnt += cal_b(i, j);
			ans = max(ans, cnt);
			int cnt2 = cal_w2(i, j);
			cnt2 += cal_b2(i, j);
			ans = max(ans, cnt2);
			if (cnt == 6)
			{
				i++;
				i--;
			}
		}
	}
	cout << ans << endl;
}

int main()
{

	solve();
	// system("pause");
	return 0;
}


Measured memory_bytes: 83456