#include <bits/stdc++.h>
#include <unordered_map>

//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

#define rep(i,n) for(i=1;i<=n;i++)
#define Rep(i,n) for(i=0;i<n;i++)
#define For(i,a,b) for(i=a;i<=b;i++)

#define pb(x) push_back(x)
#define sz(x) (int)x.size()

#define mem(ara,val) memset(ara,val,sizeof(ara))
#define eps 1e-9

#define si(x) scanf("%d",&x)
#define sii(x,y) scanf("%d %d",&x,&y)
#define siii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sl(x) scanf("%lld",&x)
#define sll(x,y) scanf("%lld %lld",&x,&y)
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define ss(str) scanf("%s",str)
#define pi(x) printf("%d",x)
#define pii(x,y) printf("%d %d",x,y)
#define piii(x,y,z) printf("%d %d %d",x,y,z)
#define pl(x) printf("%lld",x)
#define pll(x,y) printf("%lld %lld",x,y)
#define plll(x,y,z) printf("%lld %lld %lld",x,y,z)
#define NL printf("\n")
#define Max 505
#define INF 1e9
#define un(x) x.erase(unique( x.begin() , x.end() ), x.end())
#define mod 1000000007

#define FI freopen("in.txt","r",stdin)

#define D(x) cout << #x << " = " << x << endl
#define DD(x,y) cout << #x << " = " << x << "   " << #y << " = " << y << endl
#define DDD(x,y,z) cout << #x << " = " << x << "   " << #y << " = " << y << "   " << #z << " = " << z << endl

typedef long long LL;
typedef unsigned long long ULL;

using namespace std;

int setb(int n,int pos)
{
    return n=n | (1 << pos);
}
int resb(int n,int pos)
{
    return n=n & ~(1 << pos);
}
bool checkb(int n,int pos)
{
    return (bool)(n & (1 << pos));
}

LL bigmod(LL b,LL p,LL m)
{
    if( p == 0 ) return 1;
    LL ret = bigmod(b,p/2,m);
    ret = ( ret * ret ) % m;
    if( p & 1 ) ret = ( ret * b ) % m;
    return ret;
}

int ara[2][2005][2005],sum[2][2005][2005];
int n,K,N;

void prework()
{
    for(int k = 0; k < 2; k++)
    {
        for(int i = 1; i <= N; i++)
        {
            for(int j = 1; j <= N; j++)
            {
                sum[k][i][j] = sum[k][i-1][j] + sum[k][i][j-1] - sum[k][i-1][j-1] + ara[k][i][j];
            }
        }
    }
}

inline int Q(int k,int x1,int y1,int x2,int y2)
{
    int ret = sum[k][x2][y2] - sum[k][x2][y1-1] - sum[k][x1-1][y2] + sum[k][x1-1][y1-1];
    return ret;
}


inline int white(int x,int y)
{
    int x1 = x;
    int y1 = y;
    int x2 = x + K - 1;
    int y2 = y + K - 1;
    if( x1 < 1 ) x1 = 1;
    if( y1 < 1 ) y1 = 1;
    if( x2 > N ) x2 = N;
    if( y2 > N ) y2 = N;
    if( x1 > N || y1 > N || x2 < 1 || y2 < 1 ) return 0;
    return Q(0,x1,y1,x2,y2);
}

inline int black(int x,int y)
{
    int x1 = x;
    int y1 = y;
    int x2 = x + K - 1;
    int y2 = y + K - 1;
    if( x1 < 1 ) x1 = 1;
    if( y1 < 1 ) y1 = 1;
    if( x2 > N ) x2 = N;
    if( y2 > N ) y2 = N;
    if( x1 > N || y1 > N || x2 < 1 || y2 < 1 ) return 0;
    return Q(1,x1,y1,x2,y2);
}

int solve()
{
    int ret = 0;
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            int tot = 0;
            ///white
            tot += white(i,j);
            tot += white(i-K,j-K);
            tot += white(i-K,j+K);
            tot += white(i+K,j-K);
            tot += white(i+K,j+K);
            tot += white(i-N,j);
            tot += white(i+N,j);
            tot += white(i,j-N);
            tot += white(i,j+N);
            ///black
            tot += black(i-K,j);
            tot += black(i+K,j);
            tot += black(i,j-K);
            tot += black(i,j+K);

            ret = max( ret ,tot );
        }
    }


    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            int tot = 0;
            ///black
            tot += black(i,j);
            tot += black(i-K,j-K);
            tot += black(i-K,j+K);
            tot += black(i+K,j-K);
            tot += black(i+K,j+K);
            tot += black(i-N,j);
            tot += black(i+N,j);
            tot += black(i,j-N);
            tot += black(i,j+N);
            ///white
            tot += white(i-K,j);
            tot += white(i+K,j);
            tot += white(i,j-K);
            tot += white(i,j+K);

            ret = max( ret ,tot );
        }
    }

    return ret;
}

int main()
{
   // FI;
    int i,j,x,y;
    char str[105];
    sii(n,K);
    N = K + K;
    rep(i,n)
    {
        sii(x,y);
        ss(str);
        x %= N;
        y %= N;
        x++;
        y++;
        int c = 0;
        if( str[0] == 'B' ) c = 1;
        ara[c][x][y]++;
    }
    prework();
    int ret = solve();
    pi(ret); NL;
    return 0;
}


Measured memory_bytes: 62976