#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sys/time.h>
#include <cmath>
#include <tuple>
#include <queue>
#include <bitset>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define rep(i, n) FOR(i, 0, n)

template <class T>
std::vector<std::vector<T>> Imos2D(const std::vector<std::vector<T>>& a) {
    int h = a.size(), w = a[0].size();
    std::vector<std::vector<T>> s(h + 1, std::vector<T>(w + 1, 0));
    rep(i, h) rep(j, w) s[i + 1][j + 1] = a[i][j];
    rep(i, h + 1) rep(j, w) s[i][j + 1] += s[i][j];
    rep(i, h) rep(j, w + 1) s[i + 1][j] += s[i][j];
    return s;
}

template <class T>
int sum(const std::vector<std::vector<T>>& s, int i, int j, int h, int w) {
    return s[i + h][j + w] - s[i][j + w] + s[i][j] - s[i + h][j];
}

void solve(long long N, long long K, vector<long long> x, vector<long long> y, vector<string> c){
    vector< vector<long long> > bl(K*2, vector<long long> (K*2, 0));
    vector< vector<long long> > wh(K*2, vector<long long> (K*2, 0));
    for (int i = 0; i < N; i++) {
        if (c[i] == "W") {
            wh[x[i]%(K*2)][y[i]%(K*2)]++;
        } else {
            bl[x[i]%(K*2)][y[i]%(K*2)]++;
        }
    }
    vector<vector<long long> > imoswh = Imos2D<long long>(wh);
    vector<vector<long long> > imosbl = Imos2D<long long>(bl);

    long long ret = 0;
    for (int i = 0; i < K*2; i++) {
        for (int j = 0; j < K; j++) {
            long long tmp = 0;
            if (i < K) {
                tmp += sum(imosbl, i, j, K, K);
                tmp += sum(imosbl, 0, 0, i, j);
                tmp += sum(imosbl, 0, j+K, i, K-j);
                tmp += sum(imosbl, i+K, 0, K-i, j);
                tmp += sum(imosbl, i+K, j+K, K-i, K-j);
                tmp += sum(imoswh, i, 0, K, j);
                tmp += sum(imoswh, 0, j, i, K);
                tmp += sum(imoswh, i+K, j, K-i, K);
                tmp += sum(imoswh, i, j+K, K, K-j);
            } else {
                int I = i - K;
                tmp += sum(imoswh, I, j, K, K);
                tmp += sum(imoswh, 0, 0, I, j);
                tmp += sum(imoswh, 0, j+K, I, K-j);
                tmp += sum(imoswh, I+K, 0, K-I, j);
                tmp += sum(imoswh, I+K, j+K, K-I, K-j);
                tmp += sum(imosbl, I, 0, K, j);
                tmp += sum(imosbl, 0, j, I, K);
                tmp += sum(imosbl, I+K, j, K-I, K);
                tmp += sum(imosbl, I, j+K, K, K-j);
            }
            if (tmp > ret) {
                ret = tmp;
            }
        }
    }
    std::cout << ret << std::endl;
}

int main(){	
    struct timeval start,end;
    long long span;
    gettimeofday(&start,NULL);

    long long K;
    long long N;
    scanf("%lld",&N);
    vector<long long> x(N-1+1);
    vector<long long> y(N-1+1);
    vector<string> c(N-1+1);
    scanf("%lld",&K);
    for(int i = 0 ; i <= N-1 ; i++){
    	scanf("%lld",&x[i]);
    	scanf("%lld",&y[i]);
    	cin >> c[i];
    }
    solve(N, K, x, y, c);

    gettimeofday(&end,NULL);
    span = (end.tv_sec -start.tv_sec)*1000000LL + (end.tv_usec - start.tv_usec);
    std::cerr << "--Total Time: " << span/1000 << "ms" << endl;
    return 0;
}


Measured memory_bytes: 133504