#include <cstdio>
#include <iostream>
#include <array>
#include <vector>
#include <deque>
#include <string>
#include <algorithm>
#include <functional>
#include <tuple>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <utility>
#include <memory>
#include <valarray>
#include <climits>

using namespace std;

using ll = long long int;

#define REP(i, N) for (int i = 0; i < (N); ++i)
#define scanInt(x) int x; scanf("%d", &(x)); getchar();
#define scanLLInt(x) ll x; scanf("%lld", &(x)); getchar();
#define scanVecInt(A, N) vector<int> A(N); REP(i, N){scanf("%d", &(A[i])); getchar();}
#define scanVecLLInt(A, N) vector<ll> A(N); REP(i, N){scanf("%lld", &(A[i])); getchar();}

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 i1, int j1, int i2, int j2) {
    return s[i2][j2] - s[i1][j2] + s[i1][j1] - s[i2][j1];
}

int main()
{
    scanInt(N);
    scanInt(K);
    const auto K2 = K * 2;
    vector<vector<int>> wboard(K2, vector<int>(K2, 0));
    vector<vector<int>> bboard(K2, vector<int>(K2, 0));
    REP(i, N){
        scanInt(xi);
        scanInt(yi);
        bool isWhite = (getchar() == 'W'); getchar();
        if (isWhite){
            ++wboard[xi % K2][yi % K2];
        }else{
            ++bboard[xi % K2][yi % K2];
        }
    }
    auto wimos = Imos2D(wboard);
    auto bimos = Imos2D(bboard);
    // auto get_sum = [&](vector<vector<int>>& s, int i, int j, int h, int w){
    //     return s[(i + h) % (K2 + 1)][(j + w) % (K2 + 1)] - s[i % (K2 + 1)][(j + w) % (K2 + 1)] + s[i % (K2 + 1)][j % (K2 + 1)] - s[(i + h) % (K2 + 1)][j % (K2 + 1)];
    // };
    int result = 0;
    REP(xd, K){
        REP(yd, K){
            auto wsum =
                sum(wimos, xd, yd, xd + K, yd + K)
                + sum(wimos, xd + K, yd + K, K2, K2)
                + sum(wimos, 0, 0, xd, yd)
                + sum(wimos, xd + K, 0, K2, yd)
                + sum(wimos, 0, yd + K, xd, K2);
            auto bsum =
                sum(bimos, xd, 0, xd + K, yd)
                + sum(bimos, 0, yd, xd, yd + K)
                + sum(bimos, xd + K, yd, K2, yd + K)
                + sum(bimos, xd, yd + K, xd + K, K2);
            result = max(result, wsum + bsum);
        }
    }
    REP(xd, K){
        REP(yd, K){
            auto bsum =
                sum(bimos, xd, yd, xd + K, yd + K)
                + sum(bimos, xd + K, yd + K, K2, K2)
                + sum(bimos, 0, 0, xd, yd)
                + sum(bimos, xd + K, 0, K2, yd)
                + sum(bimos, 0, yd + K, xd, K2);
            auto wsum =
                sum(wimos, xd, 0, xd + K, yd)
                + sum(wimos, 0, yd, xd, yd + K)
                + sum(wimos, xd + K, yd, K2, yd + K)
                + sum(wimos, xd, yd + K, xd + K, K2);
            result = max(result, wsum + bsum);
        }
    }
    printf("%d\n", result);
}


Measured memory_bytes: 63104