#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;

template<typename T>
class Imos2D {
    std::vector< std::vector<T> > cumsum;
public:
    Imos2D(const std::vector< std::vector<T> >& a) {
        int h = a.size(), w = a[0].size();
        cumsum.assign(h+1, std::vector<T>(w+1, 0));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                cumsum[i+1][j+1] = a[i][j];
            }
        }
        for (int i = 1; i < h+1; i++) {
            for (int j = 0; j < w; j++) {
                cumsum[i][j+1] += cumsum[i][j];
            }
        }
        for (int i = 0; i < h; i++) {
            for (int j = 1; j < w+1; j++) {
                cumsum[i+1][j] += cumsum[i][j];
            }
        }
    };
    T sum(int i, int j, int h, int w) {
        return (cumsum[i+h][j+w] - cumsum[i][j+w]) - (cumsum[i+h][j] - cumsum[i][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)]++;
        }
    }

    Imos2D<long long> imoswh = Imos2D<long long>(wh);
    Imos2D<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 += imosbl.sum(i, j, K, K);
                tmp += imosbl.sum(0, 0, i, j);
                tmp += imosbl.sum(0, j+K, i, K-j);
                tmp += imosbl.sum(i+K, 0, K-i, j);
                tmp += imosbl.sum(i+K, j+K, K-i, K-j);
                tmp += imoswh.sum(i, 0, K, j);
                tmp += imoswh.sum(0, j, i, K);
                tmp += imoswh.sum(i+K, j, K-i, K);
                tmp += imoswh.sum(i, j+K, K, K-j);
            } else {
                int I = i - K;
                tmp += imoswh.sum(I, j, K, K);
                tmp += imoswh.sum(0, 0, I, j);
                tmp += imoswh.sum(0, j+K, I, K-j);
                tmp += imoswh.sum(I+K, 0, K-I, j);
                tmp += imoswh.sum(I+K, j+K, K-I, K-j);
                tmp += imosbl.sum(I, 0, K, j);
                tmp += imosbl.sum(0, j, I, K);
                tmp += imosbl.sum(I+K, j, K-I, K);
                tmp += imosbl.sum(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