//#include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include<iomanip>
#include<unordered_set>
#include<bitset>
#include<complex>

using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

//typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

int N, K;
pii Q[2010][2010], R[2010][2010], C[2010][2010];

signed main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N >> K;
	int x, y;
	char c;
	rep(i, 0, N)
	{
		cin >> x >> y >> c;
		x = x % (2 * K);
		y = y % (2 * K);
		if (c == 'B')
		{
			Q[y][x].first++;
		}
		else
		{
			Q[y][x].second++;
		}
	}

	rep(i, 0, 2 * K)
	{
		int w = 0, b = 0;
		rep(j, 0, K)
		{
			b += Q[i][j].first;
			w += Q[i][j].second;
		}
		rep(j, 0, 2 * K)
		{
			if (j > 0)
			{
				b -= Q[i][j - 1].first;
				b += Q[i][(j + K - 1) % (2 * K)].first;
				w -= Q[i][j - 1].second;
				w += Q[i][(j + K - 1) % (2 * K)].second;
			}
			C[i][j] = mp(b, w);
		}
	}

	rep(j, 0, 2 * K)
	{
		int w = 0, b = 0;
		rep(i, 0, K)
		{
			b += Q[i][j].first;
			w += Q[i][j].second;
		}
		rep(i, 0, 2 * K)
		{
			if (i > 0)
			{
				b -= Q[i - 1][j].first;
				b += Q[(i + K - 1) % (2 * K)][j].first;
				w -= Q[i - 1][j].second;
				w += Q[(i + K - 1) % (2 * K)][j].second;
			}
			R[i][j] = mp(b, w);
		}
	}

	int ans = 0;

	int b = 0, w = 0;
	rep(i, 0, 2 * K)
	{
		rep(j, 0, 2 * K)
		{
			if (((i / K) + (j / K)) % 2 == 0)
			{
				b += Q[i][j].first;
			}
			else
			{
				w += Q[i][j].second;
			}
		}
	}

	rep(i, 0, 2 * K)
	{
		if (i > 0)
		{
			b -= C[i - 1][0].first;
			b += C[(i + K - 1) % (2 * K)][0].first;
			b -= C[(i + K - 1) % (2 * K)][K].first;
			b += C[(i + 2 * K - 1) % (2 * K)][K].first;
			w += C[i - 1][0].second;
			w -= C[(i + K - 1) % (2 * K)][0].second;
			w += C[(i + K - 1) % (2 * K)][K].second;
			w -= C[(i + 2 * K - 1) % (2 * K)][K].second;
		}
		int tb = b, tw = w;
		rep(j, 0, 2 * K)
		{
			if (j > 0)
			{
				tb -= R[i][j - 1].first;
				tb += R[i][(j + K - 1) % (2 * K)].first;
				tb -= R[(i + K) % (2 * K)][(j + K - 1) % (2 * K)].first;
				tb += R[(i + K) % (2 * K)][(j + 2 * K - 1) % (2 * K)].first;
				tw += R[i][j - 1].second;
				tw -= R[i][(j + K - 1) % (2 * K)].second;
				tw += R[(i + K) % (2 * K)][(j + K - 1) % (2 * K)].second;
				tw -= R[(i + K) % (2 * K)][(j + 2 * K - 1) % (2 * K)].second;
			}
			chmax(ans, tb + tw);
		}
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 189312