#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repo(i,n) for(int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define uniq(v) v.erase(unique(v.begin(),v.end()),v.end())
#define lb(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define ub(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
using Pair = pair<ll,pair<int,int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>> 
const ll mod=1000000007;
//const ll mod=998244353;
const ld pi=acos(-1.0);
const ll INF = 1LL<<61;
template<class T>bool chmax(T &a, const T &b) { 
  if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) {
  if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//  cout << fixed << setprecision (20);   小数点以下2０桁まで
//実行時間制約2秒では２×10^8回くらいまで計算できる




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

  ll n,k;
  cin>>n>>k;

  vector<vector<ll>> b(2*k,vector<ll>(2*k));
  vector<vector<ll>> w(2*k,vector<ll>(2*k));

  rep(i,n){
    ll x,y;
    char c;
    cin>>x>>y>>c;
    x%=2*k;
    y%=2*k;
    if(c=='B') b[x][y]++;
    if(c=='W') w[x][y]++;
  }

  vector<vector<ll>> bx(2*k,vector<ll>(2*k));
  vector<vector<ll>> by(2*k,vector<ll>(2*k));
  rep(i,k) rep(j,k) bx[i][0]+=b[i][j];
  rep(i,k) rep(j,k) by[0][i]+=b[j][i];

  rep(i,2*k){
    rep(j,2*k){
      bx[i][j]=bx[i][(j+2*k-1)%(2*k)]+b[i][(j+k-1)%(2*k)]-b[i][(j+2*k-1)%(2*k)];
    }
  }
  rep(i,2*k){
    rep(j,2*k){
      by[i][j]=by[(i+2*k-1)%(2*k)][j]+b[(i+k-1)%(2*k)][j]-b[(i+2*k-1)%(2*k)][j];
    }
  }


  vector<vector<ll>> wx(2*k,vector<ll>(2*k));
  vector<vector<ll>> wy(2*k,vector<ll>(2*k));
  rep(i,k) rep(j,k) wx[i][0]+=w[i][j];
  rep(i,k) rep(j,k) wy[0][i]+=w[j][i];

  rep(i,2*k){
    rep(j,2*k){
      wx[i][j]=wx[i][(j+2*k-1)%(2*k)]+w[i][(j+k-1)%(2*k)]-w[i][(j+2*k-1)%(2*k)];
    }
  }
  rep(i,2*k){
    rep(j,2*k){
      wy[i][j]=wy[(i+2*k-1)%(2*k)][j]+w[(i+k-1)%(2*k)][j]-w[(i+2*k-1)%(2*k)][j];
    }
  }


  ll ans=0;  
  vector<vector<ll>> cnt(2*k,vector<ll>(2*k));

  rep(i,2*k){
    rep(j,2*k){
      if(i<k&&j<k) cnt[0][0]+=b[i][j];
      if(i<k&&k<=j) cnt[0][0]+=w[i][j];
      if(k<=i&&j<k) cnt[0][0]+=w[i][j];
      if(k<=i&&k<=j) cnt[0][0]+=b[i][j];      
    }
  }

  rep(i,2*k){
    rep(j,2*k){
      if(i==0&&j==0) continue;
      if(j!=0) {
        cnt[i][j]=cnt[i][j-1];
        cnt[i][j]+=by[i][(j+k-1)%(2*k)]-by[i][j-1];
        cnt[i][j]-=wy[i][(j+k-1)%(2*k)]-wy[i][j-1];
        cnt[i][j]+=by[(i+k)%(2*k)][j-1]-by[(i+k)%(2*k)][(j+k-1)%(2*k)];
        cnt[i][j]-=wy[(i+k)%(2*k)][j-1]-wy[(i+k)%(2*k)][(j+k-1)%(2*k)];
      }
      if(j==0) {
        cnt[i][j]=cnt[i-1][j];
        cnt[i][j]+=bx[(i+k-1)%(2*k)][j]-bx[i-1][j];
        cnt[i][j]-=wx[(i+k-1)%(2*k)][j]-wx[i-1][j];
        cnt[i][j]+=bx[i-1][(j+k)%(2*k)]-bx[(i+k-1)%(2*k)][(j+k)%(2*k)];
        cnt[i][j]-=wx[i-1][(j+k)%(2*k)]-wx[(i+k-1)%(2*k)][(j+k)%(2*k)];
      }
      chmax(ans,cnt[i][j]);
    }
  }
/*
  rep(i,2*k){
    rep(j,2*k){
      cout << cnt[i][j] << " ";
    }
    cout << endl;
  }
*/
  cout << ans << endl;
}

Measured memory_bytes: 222544