#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
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; }
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void _main()
{
    int N, K;
    cin >> N >> K;
    
    vector<vector<int>> black(K * 2, vector<int>(K * 2, 0));
    vector<vector<int>> white(K * 2, vector<int>(K * 2, 0));
    rep(i, 0, N)
    {
        int x, y;
        char c;
        cin >> x >> y >> c;
        if (c == 'B')
        {
            ++black[x % (K * 2)][y % (K * 2)];
        }
        else
        {
            ++white[x % (K * 2)][y % (K * 2)];
        }
    }

    vector<vector<int>> blackSum(K * 2 + 1, vector<int>(K * 2 + 1, 0));
    vector<vector<int>> whiteSum(K * 2 + 1, vector<int>(K * 2 + 1, 0));
    rep(i, 0, K * 2)
    {
        rep(j, 0, K * 2)
        {
            blackSum[i + 1][j + 1] = blackSum[i + 1][j] + blackSum[i][j + 1] - blackSum[i][j] + black[i][j];
            whiteSum[i + 1][j + 1] = whiteSum[i + 1][j] + whiteSum[i][j + 1] - whiteSum[i][j] + white[i][j];
        }
    }

    auto PrintVV = [](const vector<vector<int>>& vv)
    {
        for (const auto& v : vv)
        {
            for (const int i : v)
            {
                cout << i << " ";
            }
            cout << endl;
        }
        cout << endl;
    };

    //PrintVV(blackSum);
    //PrintVV(whiteSum);

    int ans = 0;
    rep(i, 0, K * 2)
    {
        rep(j, 0, K * 2)
        {
            // [l, r), [b, t)
            int l = i;
            int r = (i + K) % (K * 2);
            if (l > r) swap(l, r);
            int b = j;
            int t = (j + K) % (K * 2);
            if (b > t) swap(b, t);

            auto f = [](const vector<vector<int>>& sum, int l, int r, int b, int t)
            {
                //cout << l << " " << r << " " << b << " " << t << endl;
                return sum[r][t] - sum[l][t] - sum[r][b] + sum[l][b];
            };

            // 6|7|8
            // -----
            // 3|4|5
            // -----
            // 0|1|2
            rep(k, 0, 2)
            {
                int score = 0;
                score += f(k == 0 ? blackSum : whiteSum, 0, l, 0, b); // 0
                score += f(k == 0 ? whiteSum : blackSum, l, r, 0, b); // 1
                score += f(k == 0 ? blackSum : whiteSum, r, K * 2, 0, b); // 2
                score += f(k == 0 ? whiteSum : blackSum, 0, l, b, t); // 3
                score += f(k == 0 ? blackSum : whiteSum, l, r, b, t); // 4
                score += f(k == 0 ? whiteSum : blackSum, r, K * 2, b, t); // 5
                score += f(k == 0 ? blackSum : whiteSum, 0, l, t, K * 2); // 6
                score += f(k == 0 ? whiteSum : blackSum, l, r, t, K * 2); // 7
                score += f(k == 0 ? blackSum : whiteSum, r, K * 2, t, K * 2); // 8

                if (chmax(ans, score))
                {
                    //cout << i << " " << j << endl;
                }
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 63104