#include <bits/stdc++.h>

#define VARNAME(x) #x
#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using ld = long double;
template <typename T>
vector<T> Vec(int n, T v)
{
    return vector<T>(n, v);
}
template <class... Args>
auto Vec(int n, Args... args)
{
    auto val = Vec(args...);
    return vector<decltype(val)>(n, move(val));
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz:" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = (ll)1e9 + 7LL;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, K;
    cin >> N >> K;
    vector<vector<ll>> wsum(2 * K, vector<ll>(2 * K, 0LL));
    vector<vector<ll>> bsum(2 * K, vector<ll>(2 * K, 0LL));
    for (int i = 0; i < N; i++) {
        ll x, y;
        char c;
        cin >> x >> y >> c;
        x %= (2LL * K);
        y %= (2LL * K);
        if (c == 'B') {
            bsum[y][x]++;
        } else {
            wsum[y][x]++;
        }
    }
    for (int i = 1; i < 2 * K; i++) {
        for (int j = 0; j < 2 * K; j++) {
            bsum[i][j] += bsum[i - 1][j];
            wsum[i][j] += wsum[i - 1][j];
        }
    }
    for (int i = 0; i < 2 * K; i++) {
        for (int j = 1; j < 2 * K; j++) {
            bsum[i][j] += bsum[i][j - 1];
            wsum[i][j] += wsum[i][j - 1];
        }
    }
    auto getrect = [&](const vector<vector<ll>>& sum, ll yinf, ll xinf, ll ysup, ll xsup) {
        if (xinf >= xsup or yinf >= ysup) {
            return 0LL;
        }
        ll ans = sum[ysup - 1][xsup - 1];
        if (yinf > 0) {
            ans -= sum[yinf - 1][xsup - 1];
        }
        if (xinf > 0) {
            ans -= sum[ysup - 1][xinf - 1];
        }
        if (yinf > 0 and xinf > 0) {
            ans += sum[yinf - 1][xinf - 1];
        }
        return ans;
    };

    auto wget = [&](const int x, const int y) {
        const ll w = getrect(wsum, x, y, x + K, y + K) + getrect(wsum, 0, 0, x, y) + getrect(wsum, 0, y + K, x, 2 * K) + getrect(wsum, x + K, 0, 2 * K, y) + getrect(wsum, x + K, y + K, 2 * K, 2 * K);
        const ll b = getrect(bsum, x, 0, x + K, y) + getrect(bsum, x + K, y, 2 * K, y + K) + getrect(bsum, x, y + K, x + K, 2 * K) + getrect(bsum, 0, y, x, y + K);
        return b + w;
    };
    auto bget = [&](const int x, const int y) {
        const ll b = getrect(bsum, x, y, x + K, y + K) + getrect(bsum, 0, 0, x, y) + getrect(bsum, 0, y + K, x, 2 * K) + getrect(bsum, x + K, 0, 2 * K, y) + getrect(bsum, x + K, y + K, 2 * K, 2 * K);
        const ll w = getrect(wsum, x, 0, x + K, y) + getrect(wsum, x + K, y, 2 * K, y + K) + getrect(wsum, x, y + K, x + K, 2 * K) + getrect(wsum, 0, y, x, y + K);
        return b + w;
    };
    ll maxi = 0;
    for (int i = 0; i <= K; i++) {
        for (int j = 0; j <= K; j++) {
            maxi = max({maxi, wget(i, j), bget(i, j)});
        }
    }
    cout << maxi << endl;

    return 0;
}


Measured memory_bytes: 62976