#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);++i)
#define rrep(i,n) for(int (i)=(n)-1;(i)>=0;--i)
#define rep1(i,n) for(int (i)=1;(i)<=(n);++i)
#define rrep1(i,n) for(int (i)=(n);(i)>=1;--i)
#define pb push_back
#define fr first
#define sc second
typedef long long ll;
typedef pair<int,int> P;
typedef pair<long long,long long> LP;
typedef double db;
using namespace std;
ll N,K,X[100001],Y[100001];
ll B[100001];
ll b[2001][2001];
ll w[2001][2001];
ll ib[2001][2001];
ll iw[2001][2001];
ll KK;
ll g[2001][2001];

bool valid(ll x){
  return (0<=x && x<KK);
}

ll IB(ll y,ll x){
  if(valid(y) && valid(x)) return ib[y][x];
  else return 0;
}

ll IW(ll y,ll x){
  if(valid(y) && valid(x)) return iw[y][x];
  else return 0;
}

ll SW(ll y2,ll x2,ll y1,ll x1){
  // for(int i = max(0LL,y1);i<=min(KK-1,y2);i++)
  //   for(int j = max(0LL,x1);j<=min(KK-1,x2);j++) g[i][j]=0;

  return IW(y2,x2) - IW(y2,x1-1) - IW(y1-1,x2) + IW(y1-1,x1-1);
}

ll SB(ll y2,ll x2,ll y1,ll x1){
  // for(ll i = max(0LL,y1);i<=min(KK-1,y2);i++) for(ll j = max(0LL,x1);j<=min(KK-1,x2);j++) g[i][j]=1;
  // cout<<max(0LL,y1)<<"a"<<min(KK-1,y2)<<"b"<<max(0LL,x1)<<"c"<<min(KK-1,x2)<<endl;
  // cout<<y2<<" "<<x2<<" "<<y1<<" "<<x1<<endl;
  return IB(y2,x2) - IB(y2,x1-1) - IB(y1-1,x2) + IB(y1-1,x1-1);
}


ll calc(ll y,ll x){
  // cout<<KK<<"~~~~~~~~~~~~~~~"<<endl;
  ll res = 0;
  // rep(i,KK) rep(j,KK) g[i][j] = 0;
  // cout<<"abs "<<KK-1<<" abw "<<x+K-1<<endl;
  res += SB(y+K-1,min(KK-1,x+K-1),y,x)-SW(y+K-1,min(KK-1,x+K-1),y,x);
  if(y>0 && x>0) res+= SB(y-1,x-1,0,x-K)-SW(y-1,x-1,0,x-K);
  if(x>0) res += SB(KK-1,x-1,y+K,x-K)-SW(KK-1,x-1,y+K,x-K);
  if(x<K && y>0) res += SB(y-1,KK-1,0,x+K)-SW(y-1,KK-1,0,x+K);
  if(x<K) res += SB(KK-1,KK-1,y+K,x+K)-SW(KK-1,KK-1,y+K,x+K);
  if(x>K) res += SB(y+K-1,x-K-1,y,0)-SW(y+K-1,x-K-1,y,0);
  res += SW(KK-1,KK-1,0,0);
  // cout<<y<<" "<<x<<" "<<res<<endl;
  // rep(i,10){
  //   rep(j,10) cout<<g[i][j];
  //   cout<<endl;
  // }
  // cout<<endl;
  return res;
}

int main(){
  string s;
  cin>>N>>K;
  KK = 2*K;
  rep(i,N){
    cin>>X[i]>>Y[i]>>s;
    X[i]%=KK;
    Y[i]%=KK;
    if(s[0]=='B'){
      B[i]=1;
      b[Y[i]][X[i]]++;
    }
    else{
      B[i]=0;
      w[Y[i]][X[i]]++;
    }
  }
  ib[0][0]=b[0][0];
  iw[0][0]=w[0][0];
  rep1(i,KK-1){
    ib[0][i]=b[0][i]+ib[0][i-1];
    iw[0][i]=w[0][i]+iw[0][i-1];
  }
  rep1(i,KK-1){
    ib[i][0]=b[i][0]+ib[i-1][0];
    iw[i][0]=w[i][0]+iw[i-1][0];
    rep1(j,KK-1){
      ib[i][j]=b[i][j]+ib[i-1][j]+ib[i][j-1]-ib[i-1][j-1];
      iw[i][j]=w[i][j]+iw[i-1][j]+iw[i][j-1]-iw[i-1][j-1];
    }
  }
  // rep(i,10){
  //   rep(j,10){
  //     cout<<b[i][j];
  //   }
  //   cout<<endl;
  // }
  // cout<<endl;
  // rep(i,10){
  //   rep(j,10){
  //     cout<<w[i][j];
  //   }
  //   cout<<endl;
  // }
  // cout<<endl;
  // rep(i,KK){
  //   rep(j,KK){
  //     cout<<ib[i][j];
  //   }
  //   cout<<endl;
  // }
  // cout<<endl;
  // rep(i,KK){
  //   rep(j,KK){
  //     cout<<iw[i][j];
  //   }
  //   cout<<endl;
  // }
  ll ans = 0;
  rep(i,K){
    rep(j,KK){
      ans = max(ans,calc(i,j));
    }
  }
  // cout<<calc(1LL,1LL)<<endl;
  cout<<ans<<endl;
}


Measured memory_bytes: 128256