/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Gosu_Hiroo
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define tr(container, it) \
        for (auto it = container.begin(); it != container.end(); it++)
#define scontains(c, x) ((c).find(x) != (c).end())   //O(log n)
#define contains(c, x) (find((c).begin(),(c).end(),x) != (c).end()) //O(n)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define mll map<ll,ll>
#define intv(x, a, b)((x)>=a && (x)<=b)
#define  rep(i, _begin, _end) for (__typeof(_end) end = _end, begin = _begin,  i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define  _for(i, end) for (__typeof(end) i = 0, _len = (end); i < (_len); i += 1)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define F first
#define S second
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define what_is(x) cerr << #x << " is " << x << endl;
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define bit(n) (1LL<<(n))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define readint ({int t;cin >> t;t;})
#define fcout cout << fixed << setprecision(12)
#define fcerr cerr << fixed << setprecision(12)

const double PI = 2 * acos(.0);
const int INF = 0x3f3f3f3f;
const ll LLINF = 1000000000000000005LL;;
const ll MOD = (ll) (1e9) + 7;
void _mod(ll &x){x = mod(x, MOD);}
//const ll MOD = (ll) 998244353 ;
const double EPS = 1e-10;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *) array, (T *) (array + N), val);
}

template <typename T>
ostream& operator << (ostream& os, const vector<T> V) {
    for(auto v : V)os << v << " ";
    return os;
}

template <typename T>
ostream& operator << (ostream& os, const set<T> V) {
    for(auto v : V)os << v << " ";
    return os;
}

template <typename T>
ostream& operator << (ostream& os, const deque<T> V) {
    for(auto v : V)os << v << " ";
    return os;
}

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

#define int ll

class DChecker {
public:
    int W[2022][2022], B[2022][2022];
    int WS[2022][2022], BS[2022][2022];
    int N, K;
    int cnt_B(int x2, int y2, int x1, int y1){
        if(x1 < 0 || y1 < 0 || x2 > K || y2 > K)return 0;
        return BS[x2][y2] - BS[x1][y2] - BS[x2][y1] + BS[x1][y1];
    }
    int cnt_W(int x2, int y2, int x1, int y1){
        if(x1 < 0 || y1 < 0 || x2 > K || y2 > K)return 0;
        return WS[x2][y2] - WS[x1][y2] - WS[x2][y1] + WS[x1][y1];
    }

void solve(std::istream& cin, std::ostream& cout, std::ostream& cerr) {
    cin >> N >> K;
    K *= 2;
    Fill(BS,0), Fill(WS, 0), Fill(B, 0), Fill(W, 0);
    _for(i,N){
        int x,y;
        char c;
        cin >> x >> y >> c;
        x %= K, y %= K;
        if(c == 'B')B[x][y] ++;
        else W[x][y] ++;
    }
    for (int i = 0; i < K; ++i)
        for (int j = 0; j < K; ++j)
            BS[i+1][j+1] = BS[i][j+1] + BS[i+1][j] - BS[i][j] + B[i][j],
            WS[i+1][j+1] = WS[i][j+1] + WS[i+1][j] - WS[i][j] + W[i][j];

    int ans_x, ans_y, mx = -1e6;
    _for(i,K/2+1)_for(j,K/2+1){

        int res = cnt_B(i+K/2, j + K/2, i, j) + cnt_B(i,j,0,0) + cnt_B(K,j,i+K/2,0) + cnt_B(K,K, i+K/2,j+K/2) + cnt_B(i,K,0,j+K/2)+
                cnt_W(i+K/2,j,i,0) + cnt_W(K,j+K/2,i+K/2,j) + cnt_W(i+K/2,K, i,j+K/2) + cnt_W(i,j+K/2,0,j);
      mx = max(mx, res);

    }

    _for(i,K/2+1)_for(j,K/2+1){
            int res = cnt_W(i+K/2, j + K/2, i, j) + cnt_W(i,j,0,0) + cnt_W(K,j,i+K/2,0) + cnt_W(K,K, i+K/2,j+K/2) + cnt_W(i,K,0,j+K/2)+
                      cnt_B(i+K/2,j,i,0) + cnt_B(K,j+K/2,i+K/2,j) + cnt_B(i+K/2,K, i,j+K/2) + cnt_B(i,j+K/2,0,j);
            mx = max(mx, res);

        }
    cout << mx << endl;


}


};


#undef int
int main() {

	DChecker solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
    std::ostringstream err;
	in.tie(0); ios::sync_with_stdio(0);
//	solver.solve(in, out);
    solver.solve(in, out,err);
	return 0;
}


Measured memory_bytes: 128000