//#if !defined(YCM) && 1
//#define _FORTIFY_SOURCE 0
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC target("avx")
////#pragma GCC optimize ("O3")
////#pragma GCC target ("sse4")
//#include <stdio.h>
//#endif

#include <bits/stdc++.h>
#include <unistd.h>
//#pragma GCC diagnostic ignored "-Wunused-result"
//#pragma GCC diagnostic ignored "-Wunused-function"
using namespace std;
#define ll int64_t
#define ull unsigned long long int
#define clr(a) memset(a,0,sizeof(a))
#define clr_(a) memset(a,-1,sizeof(a))
#define pb(a) push_back(a)
#define pii pair<int,int>

#define eps 1e-9
#define inf INT_MAX
#define pi acos(-1.0)
#define ff first
#define ss second
#define INF (ll)1e18
#define m_p make_pair
#define vsort(v) sort(v.begin(),v.end())
#define all(v) v.begin(),v.end()
int fx[]={0,0,-1,1,-1,1,1,-1};
int fy[]={1,-1,0,0,1,1,-1,-1};
ll lcm(ll a,ll b){return (a/__gcd(a,b))*b;}
ll bigmod(ll a,ll p,ll mod){ll ans=1;while(p){if(p&1)ans=ans*a%mod;a=a*a%mod;p>>=1;}return ans;}
ll power(ll x,ll n){if(n==0)return 1;else if(n%2==0){ll y=power(x,n/2);return y*y;}else return x*power(x,n-1);}

#ifdef shaft
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cerr << name << " is " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cerr.write(names, comma - names) << " is " << arg1 <<endl;
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif
const int N=(int)2e3+5;
const int mod=(int)1e9+7;
int black[N][N], white[N][N];
pii solve(int I, int J, int i, int j){
    
    int b = black[I][J]-black[i-1][J]-black[I][j-1]+black[i-1][j-1];
    int w = white[I][J]-white[i-1][J]-white[I][j-1]+white[i-1][j-1];
    return m_p(b,w);
}
int main()
{
    ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(100); cout.tie(nullptr); cin.tie(nullptr);
    #ifdef shaft
        freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
    #endif ///shaft
    int n, k;
    while(cin >> n >> k){
        clr(black);
        clr(white);
        int black_tot = 0, white_tot = 0;
        int K = k+k;
        for(int i = 0; i < n; i++){
            int x, y, X, Y;
            char ch;
            cin >> x >> y >> ch;
            if(ch == 'W'){
                x %= K; y %= K;
                x += 1; y += 1;
                white[x][y] += 1;
                white_tot += 1;
                continue;
            }
            x %= K; y %= K;
            x += 1; y += 1;
            black[x][y] += 1;
            black_tot += 1;
        }
        for(int i = 1; i <= K; i++){
            for(int j = 1; j <= K; j++){
                int x = black[i-1][j] + black[i][j-1] - black[i-1][j-1];
                int y = white[i-1][j] + white[i][j-1] - white[i-1][j-1];
                black[i][j] += x;
                white[i][j] += y;
            }
        }
        int res = 0;
        for(int i = 1; i <= k+1; i++){
            for(int j = 1; j <= k+1 ; j++){
                int I = i+k-1, J = j+k-1;
                int b = black[I][J]-black[i-1][J]-black[I][j-1]+black[i-1][j-1];
                int w = white[I][J]-white[i-1][J]-white[I][j-1]+white[i-1][j-1];
                pii dan = solve(K,J,I+1,j);
                pii bam = solve(i-1,J,1,j);
                pii upor = solve(I,K,i,J+1);
                pii nich = solve(I,j-1,i,1);
                int temp_b = b+dan.ss+bam.ss+upor.ss+nich.ss + (black_tot-b-dan.ff-bam.ff-upor.ff-nich.ff);
                int temp_w = w+dan.ff+bam.ff+upor.ff+nich.ff + (white_tot-w-dan.ss-bam.ss-upor.ss-nich.ss);
                //cout << i <<" "<<j<<" "<<I<<" "<<J<<" "<<b<<" "<<w <<endl;
                int temp = max(temp_b,temp_w);
                res = res < temp ? temp : res;
            }
        }
        cout << res << endl;
    }
    return 0;
}





Measured memory_bytes: 31744