/*
これを入れて実行
g++ code.cpp
./a.out
 */
 
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
const long long INF = 1LL << 62;
const ll MOD = 1e9 + 7;
 
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}
 
ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll conbinationMemo[100][100];

ll cmemoInit(){
    rep(i, 100){
        rep(j, 100){
            conbinationMemo[i][j] = -1;
        }
    }
}
 
ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
 
ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
 
//-----------------------ここから-----------
int main(void){
    int n, k;
    cin >> n >> k;

    vector<pair<pair<int,int>,char>> a(n);
    vector<pair<int,int>> modk(n);
    rep(i, n){
        cin >> a[i].first.first >> a[i].first.second >> a[i].second;
        modk[i] = {a[i].first.first % (2 * k), a[i].first.second % (2 * k)};
    }

    vector<vector<int>> bmodk(2 * k, vector<int>(2 * k, 0));
    vector<vector<int>> wmodk(2 * k, vector<int>(2 * k, 0));
    rep(i, n){
        if(a[i].second == 'B'){
            bmodk[modk[i].first][modk[i].second]++;
        } else {
            wmodk[modk[i].first][modk[i].second]++;
        }
    }


    vector<vector<int>> bsum(2 * k + 1, vector<int>(2 * k + 1, 0));
    vector<vector<int>> wsum(2 * k + 1, vector<int>(2 * k + 1, 0));

    for(int i = 1; i < 2 * k + 1; i++){
        for(int j = 0; j < 2 * k; j++){
            bsum[i][j + 1] = bsum[i][j] + bmodk[i - 1][j];
            wsum[i][j + 1] = wsum[i][j] + wmodk[i - 1][j];
        }
    }

    for(int i = 0; i < 2 * k + 1; i++){
        for(int j = 0; j < 2 * k; j++){
            bsum[j + 1][i] += bsum[j][i];
            wsum[j + 1][i] += wsum[j][i];
        }
    }


    // rep(i, 2 * k){
    //     rep(j, 2 * k){
    //         cout << bmodk[i][j];
    //     }
    //     cout << endl;
    // }

    // for(int i = 0; i < 2 * k + 1; i++){
    //     for(int j = 0; j < 2 * k + 1; j++){
    //         cout << bsum[i][j];
    //     }
    //     cout << endl;
    // }

    // cout << "=====" << endl;

    //     rep(i, 2 * k){
    //     rep(j, 2 * k){
    //         cout << wmodk[i][j];
    //     }
    //     cout << endl;
    // }

    // for(int i = 0; i < 2 * k + 1; i++){
    //     for(int j = 0; j < 2 * k + 1; j++){
    //         cout << wsum[i][j];
    //     }
    //     cout << endl;
    // }

    int ans = 0;
    for(int i = 0; i <= k; i++){
        for(int j = 0; j <= k; j++){
            int si = i + k;
            int sj = j + k;
            int bi1 = i;
            int bj1 = j + k;
            int bi2 = i + k;
            int bj2 = j;

            int bs = bsum[si][sj] - bsum[bi1][bj1] - bsum[bi2][bj2] + bsum[i][j];
            int bsec1 = bsum[i][j];
            int bsec2 = bsum[2*k][j] - bsum[bi1][bj1];
            int bsec3 = bsum[i][2*k] - bsum[bi2][bj2];
            int bsec4 = bsum[2*k][2*k] - bsum[si][2*k] - bsum[2*k][sj] + bsum[si][sj];
            int ws = wsum[si][sj] - wsum[bi1][bj1] - wsum[bi2][bj2] + wsum[i][j];
            int wsec1 = wsum[i][j];
            int wsec2 = wsum[2*k][j] - wsum[bi1][bj1];
            int wsec3 = wsum[i][2*k] - wsum[bi2][bj2];
            int wsec4 = wsum[2*k][2*k] - wsum[si][2*k] - wsum[2*k][sj] + wsum[si][sj];
            int wtmp = wsum[2 * k][2 * k] - (ws + wsec1 + wsec2 + wsec3 + wsec4);
            ans = max(ans, bs + bsec1 + bsec2 + bsec3 + bsec4 + wtmp);
            int btmp = bsum[2 * k][2 * k] - (bs + bsec1 + bsec2 + bsec3 + bsec4);
            ans = max(ans, ws + wsec1 + wsec2 + wsec3 + wsec4 + btmp);
            //cout << i << " " << j << " " << bs <<" "<< ws << endl;


        }
    }

    
    cout << ans << endl;
}


Measured memory_bytes: 65024