/*
        _            _                  _            _       _     _         _   _          _          _             _        
       / /\         / /\               / /\         / /\    / /\  /\ \      /\_\/\_\ _     /\ \       /\ \     _    /\ \      
      / /  \       / /  \             / /  \       / / /   / / /  \ \ \    / / / / //\_\   \ \ \     /  \ \   /\_\ /  \ \     
     / / /\ \__   / / /\ \           / / /\ \__   / /_/   / / /   /\ \_\  /\ \/ \ \/ / /   /\ \_\   / /\ \ \_/ / // /\ \_\    
    / / /\ \___\ / / /\ \ \         / / /\ \___\ / /\ \__/ / /   / /\/_/ /  \____\__/ /   / /\/_/  / / /\ \___/ // / /\/_/    
    \ \ \ \/___// / /  \ \ \        \ \ \ \/___// /\ \___\/ /   / / /   / /\/________/   / / /    / / /  \/____// / / ______  
     \ \ \     / / /___/ /\ \        \ \ \     / / /\/___/ /   / / /   / / /\/_// / /   / / /    / / /    / / // / / /\_____\ 
 _    \ \ \   / / /_____/ /\ \   _    \ \ \   / / /   / / /   / / /   / / /    / / /   / / /    / / /    / / // / /  \/____ / 
/_/\__/ / /  / /_________/\ \ \ /_/\__/ / /  / / /   / / /___/ / /__ / / /    / / /___/ / /__  / / /    / / // / /_____/ / /  
\ \/___/ /  / / /_       __\ \_\\ \/___/ /  / / /   / / //\__\/_/___\\/_/    / / //\__\/_/___\/ / /    / / // / /______\/ /   
 \_____\/   \_\___\     /____/_/ \_____\/   \/_/    \/_/ \/_________/        \/_/ \/_________/\/_/     \/_/ \/___________/    
                                                                                                                              
*/
#include<bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for(int i = 0;i < (n);++i)

#define endl '\n'
#define pb emplace_back
#define mkp make_pair
#define p_q priority_queue
#define INF 1000000000
#define LLINF 1145141919810LL
#define PI 3.1415926
#define coutdb cout << fixed << setprecision(10)

const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};
const int dx_8[]={1,1,0,-1,-1,-1,0,1}, dy_8[]={0,-1,-1,-1,0,1,1,1};
const long long MOD = 1e9+7;

// typedef long long ll;
#define int long long

template <typename T> inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;}
template <typename T> inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;}
template <typename T> inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
template <typename T> inline T nHr(T a,T b){return nCr(a+b-1,b);}

int sum[2001][2001];

signed main(void){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K; cin >> N >> K;
  int x, y; char z;

  REP(i, N){
    cin >> x >> y >> z;
    if(z == 'W') x += K;
    x %= (2*K); y %= (2*K);
    sum[x+1][y+1]++;
  }

  REP(i, 2*K) REP(j, 2*K){
    sum[i+1][j+1] += sum[i][j+1] + sum[i+1][j] - sum[i][j];
  }

  int ans = 0;

  for(int i=1;i<=2*K;i++) for(int j=1;j<=2*K;j++){
    bool xout = (i>K), yout = (j>K);
    int num = 0;
    if(!xout and !yout){
      num += sum[i+K-1][j+K-1]-sum[i+K-1][j-1]-sum[i-1][j+K-1]+sum[i-1][j-1];
      num += sum[2*K][2*K]-sum[2*K][j+K-1]-sum[i+K-1][2*K]+sum[i+K-1][j+K-1];
      num += sum[i-1][j-1];
      num += sum[2*K][j-1] - sum[i+K-1][j-1];
      num += sum[i-1][2*K] - sum[i-1][j+K-1];
    } else if(xout and !yout){
      int p = i-K-1;
      num += sum[2*K][j+K-1]-sum[2*K][j-1]-sum[i-1][j+K-1]+sum[i-1][j-1];
      num += sum[p][j+K-1]-sum[p][j-1];
      num += sum[i-1][j-1]-sum[p][j-1];
      num += sum[i-1][2*K]-sum[i-1][j+K-1]-sum[p][2*K]+sum[p][j+K-1];
    } else if(!xout and yout){
      int q = j-K-1;
      num += sum[i+K-1][2*K]-sum[i+K-1][j-1]-sum[i-1][2*K]+sum[i-1][j-1];
      num += sum[i+K-1][q]-sum[i-1][q];
      num += sum[i-1][j-1]-sum[i-1][q];
      num += sum[2*K][j-1]-sum[2*K][q]-sum[i+K-1][j-1]+sum[i+K-1][q];
    } else {
      int p = i-K-1, q = j-K-1;
      num += sum[2*K][2*K]-sum[2*K][j-1]-sum[i-1][2*K]+sum[i-1][j-1];
      num += sum[p][q];
      num += sum[p][2*K] - sum[p][j-1];
      num += sum[2*K][q] - sum[i-1][q];
      num += sum[i-1][j-1] - sum[i-1][q] - sum[p][j-1] + sum[p][q];
    }
    ans = max(ans, num);
  }
  cout << ans << endl;
}

Measured memory_bytes: 31488