#include <vector>
#include <iostream>
#include <cmath>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <fstream>
#include <unistd.h>
#include <string>
#include <numeric>
#include <queue>
#include <deque>
#include <sstream>
#include <iomanip>
#include <set>
using namespace std;
typedef long long ll;
typedef vector<unsigned short> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<P> vp;
typedef vector<PL> vpl;
typedef vector<vector<P>> vvp;
typedef vector<vector<PL>> vvpl;
const int INF = 1001001001;
const ll LINF = 1e18;
const double pi = 3.1415926535897932;
const string endstr = "\n";
#define FOR(i, a, b) for(ll i = (a); i < b; i++)
#define REP(i, n) for(ll i = 0; i < n; i++)
#define FORMAP(it, m) for(auto it = m.begin(); it != m.end(); it++)
#define ff first
#define ss second
#define pb push_back

template <typename T>
T gcd(T a, T b) {
    return (a == 0) ? b : gcd(b%a, a);
}
template <typename T>
T lcm(T a, T b) {
    return a / gcd(a, b) * b;
}

bool p_comp_fs(const PL p1, const PL p2){ return p1.first < p2.first;};
bool p_comp_fg(const PL p1, const PL p2){ return p1.first > p2.first;};
bool p_comp_ss(const PL p1, const PL p2){ return p1.second < p2.second;};
bool p_comp_sg(const PL p1, const PL p2){ return p1.second > p2.second;};
template <typename T>
vector<T> uniquen(vector<T> vec){
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    return vec;
}

int sum_cnt[2002][2002][2];
int cnt[2002][2002][2];

int main(){
    REP(i, 2002) REP(j, 2002) REP(k, 2){
        sum_cnt[i][j][k] = 0; cnt[i][j][k] = 0;
    }
    ll n, k; cin >> n >> k; ll len = 2 * k;

    vl xs(n), ys(n); vc cs(n); REP(i, n) cin >> xs[i] >> ys[i] >> cs[i];
    REP(i, n){
        ll color = (cs[i] == 'B' ? 0 : 1);
        cnt[xs[i]%len][ys[i]%len][color]++;
    }

    REP(c, 2){
        REP(i, len){
            REP(j, len){
                sum_cnt[i+1][j+1][c] = sum_cnt[i][j+1][c] + sum_cnt[i+1][j][c] - sum_cnt[i][j][c] + cnt[i][j][c];
            }
        }
    }
    
    auto sum_sq = [&](ll x1, ll y1, ll x2, ll y2, ll c){
        return sum_cnt[x2][y2][c] - sum_cnt[x1][y2][c] - sum_cnt[x2][y1][c] + sum_cnt[x1][y1][c];
    };
    
    ll ans = -LINF;
    REP(c, 2){
        REP(m, k){
            REP(n, k){
                ll tmp_cnt = 0;
                tmp_cnt += sum_sq(0, 0, m, n, c);
                tmp_cnt += sum_sq(0, n+k, m, len, c);
                tmp_cnt += sum_sq(m, n, m+k, n+k, c);
                tmp_cnt += sum_sq(m+k, 0, len, n, c);
                tmp_cnt += sum_sq(m+k, n+k, len, len, c);
                tmp_cnt += sum_sq(0, n, m, n+k, 1^c);
                tmp_cnt += sum_sq(m, 0, m+k, n, 1^c);
                tmp_cnt += sum_sq(m, n+k, m+k, len, 1^c);
                tmp_cnt += sum_sq(m+k, n, len, n+k, 1^c);
                ans = max(ans, tmp_cnt);
            }
        }
    }
    cout << ans << endstr;
    return 0;
}


Measured memory_bytes: 64512