#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rrep(i,n) for(int (i)=((n)-1);(i)>=0;(i)--)
#define itn int
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
const long long INF = 1LL << 60;
const int MOD = 1000000007;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//https://www.creativ.xyz/dump-cpp-652/
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
 
// vector
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec) is >> x;
    return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}
// vector
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
    os << "{";
    repi(itr, map_var) {
        os << *itr;
        itr++;
        if (itr != map_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
// set
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
    os << "{";
    repi(itr, set_var) {
        os << *itr;
        itr++;
        if (itr != set_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
 
#define DUMPOUT cerr
 
void dump_func() {
    DUMPOUT << endl;
}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail) {
    DUMPOUT << head;
    if (sizeof...(Tail) > 0) {
        DUMPOUT << ", ";
    }
    dump_func(std::move(tail)...);
}

#ifdef DEBUG_
#define DEB
#define dump(...)                                                              \
    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
            << endl                                                            \
            << "    ",                                                         \
        dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

template <typename T>
struct ruiseki2d{
    T n, m;
    vector <vector<T>> s;
    ruiseki2d(T n,T m):n(n),m(m),s(m+1,vector<T>(n+1)){};
    void add(int y,int x,T w){ 
        s[y+1][x+1]+=w;
    }
    void build(){ 
        rep(i,n)rep(j,m) s[i+1][j+1] += s[i][j+1] + s[i+1][j] - s[i][j];
    }
    /*T sum(int y0,T x0,T y1,T x1){
        return s[y1][x1] - s[y0][x1] - s[y1][x0] +s [y0][x0];
    }*/
    T sum(int y1, int x1, int y2, int x2){
        if(y1 > y2) swap(y1,y2);
        if(x1 > x2) swap(x1,x2);
        chmin(y1, n); chmax(y1,0LL);
        chmin(y2, n); chmax(y2,0LL);
        chmin(x1, m); chmax(x1,0LL);
        chmin(x2, m); chmax(x2,0LL);
        return s[y2][x2] - s[y1][x2] - s[y2][x1] + s[y1][x1];
    }
};

signed main(void) { cin.tie(0); ios::sync_with_stdio(false);
    int n,k; cin>>n>>k;
    ruiseki2d <int> white(2*k,2*k), black(2*k,2*k);
    rep(i,n){
        int x,y;
        char c;
        cin>>x>>y>>c;
        x %= 2*k; y %= 2*k;
        if(c == 'W') white.add(y,x,1);
        else black.add(y,x,1);
    }
    white.build();
    black.build();
    int ans = 0;

    rep(i,k)rep(j,k){
        int score=0;
        score+=white.sum(i+k,j+k,2*k,2*k);
        score+=white.sum(0,j+k,i,2*k);
        score+=white.sum(i,j,i+k,j+k);
        score+=white.sum(0,0,i,j);
        score+=white.sum(i+k,0,2*k,j);
        score+=black.sum(i,j+k,i+k,2*k);
        score+=black.sum(0,j,i,j+k);
        score+=black.sum(i+k,j,2*k,j+k);
        score+=black.sum(i,0,i+k,j);
        chmax(ans,score);
        
        score=0;
        score+=black.sum(i+k,j+k,2*k,2*k);
        score+=black.sum(0,j+k,i,2*k);
        score+=black.sum(i,j,i+k,j+k);
        score+=black.sum(0,0,i,j);
        score+=black.sum(i+k,0,2*k,j);
        score+=white.sum(i,j+k,i+k,2*k);
        score+=white.sum(0,j,i,j+k);
        score+=white.sum(i+k,j,2*k,j+k);
        score+=white.sum(i,0,i+k,j);
        chmax(ans,score);
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 62976