#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll INF = 1LL<<60;

ll gcd(ll a, ll b) {
  if(b == 0) return a;
  return gcd(b, a % b);
}

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll dist_b[2010][2010] = {0};
ll dist_w[2010][2010] = {0};
ll sum_b[2010][2010]  = {0};
ll sum_w[2010][2010]  = {0};

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

  ll N, K;
  cin >> N >> K;

  ll x, y;
  string c;
  ll cnt_b = 0;
  ll cnt_w = 0;

  for(int i = 0; i < N; i++){
    cin >> x >> y >> c;
    x = x % (2*K);
    y = y % (2*K);
    if(c == "B"){
      dist_b[x][y] += 1;
      cnt_b++;
      dist_w[x][(y+K)%(2*K)] += 1;
      cnt_w++;
    }else{
      x = (x + K) % (2*K);
      dist_b[x][y] += 1;
      dist_w[x][(y+K)%(2*K)] += 1;
      cnt_b++;
      cnt_w++;
    }
  }

  sum_b[0][0] = dist_b[0][0];
  sum_w[0][0] = dist_w[0][0];

  for(int i = 1; i < 2 * K; i++){
    sum_b[0][i] = sum_b[0][i-1] + dist_b[0][i];
  }
  for(int i = 1; i < 2 * K; i++){
    sum_b[i][0] = sum_b[i-1][0] + dist_b[i][0];
  }

  for(int i = 1; i < 2 * K; i++){
    sum_w[0][i] = sum_w[0][i-1] + dist_w[0][i];
  }
  for(int i = 1; i < 2 * K; i++){
    sum_w[i][0] = sum_w[i-1][0] + dist_w[i][0];
  }


  for(int i = 1; i < 2 * K; i++){
    for(int j = 1; j < 2 * K; j++){
      sum_b[i][j] = sum_b[i-1][j] + sum_b[i][j-1] - sum_b[i-1][j-1] + dist_b[i][j];
      sum_w[i][j] = sum_w[i-1][j] + sum_w[i][j-1] - sum_w[i-1][j-1] + dist_w[i][j];      
    }
  }



  // for debug
  // for(int i = 0; i < 2 *K; i++){
  //   for(int j = 0; j < 2 *K; j++){
  //     if(dist[i][j] == 1) cout << "B";
  //     else if(dist[i][j] == 2) cout << "W";
  //     else cout << 0;
  //   }
  //   cout << endl;
  // }

  // for(int i = 0; i < 2 *K; i++){
  //   for(int j = 0; j < 2 *K; j++){
  //     cout << sum_b[i][j];
  //   }
  //   cout << endl;
  // }
  // for(int i = 0; i < 2 *K; i++){
  //   for(int j = 0; j < 2 *K; j++){
  //     cout << sum_w[i][j];
  //   }
  //   cout << endl;
  // }

  ll ans = 0;
  ll tmp_cnt_b;
  ll tmp_cnt_w;
  for(int i = 0; i <= K; i++){
    for(int j = 0; j <= K; j++){
      // 黒領域の中にある blackとwhiteの数をそれぞれ
      // 数える
      tmp_cnt_b = 0;
      tmp_cnt_w = 0;

      ll cnt_b_A = 0;
      ll cnt_b_B = 0;
      ll cnt_b_C = 0;
      ll cnt_b_D = 0;
      ll cnt_b_E = 0;
      ll cnt_b_F = 0;
      ll cnt_b_G = 0;
      ll cnt_b_H = 0;
      ll cnt_b_I = 0;
      
      ll cnt_w_A = 0;
      ll cnt_w_B = 0;
      ll cnt_w_C = 0;
      ll cnt_w_D = 0;
      ll cnt_w_E = 0;
      ll cnt_w_F = 0;
      ll cnt_w_G = 0;
      ll cnt_w_H = 0;
      ll cnt_w_I = 0;
      
      if(i-1 >= 0 && j-1 >= 0){
        cnt_b_A += sum_b[i-1][j-1];
        cnt_w_A += sum_w[i-1][j-1];
      }

      if(i-1 >= 0 && j-1+K < 2*K){
        cnt_b_B += sum_b[i-1][j-1+K];
        cnt_w_B += sum_w[i-1][j-1+K];
      }
      
      if(i-1>=0){
        cnt_b_C += sum_b[i-1][2*K-1];
        cnt_w_C += sum_w[i-1][2*K-1];
      }

      if(j-1 >= 0 && i-1+K  < 2*K){
        cnt_b_D += sum_b[i-1+K][j-1];
        cnt_w_D += sum_w[i-1+K][j-1];
      }

      if(j-1 >= 0){
        cnt_b_E += sum_b[2*K-1][j-1];
        cnt_w_E += sum_w[2*K-1][j-1];        
      }

      cnt_b_F += sum_b[i-1+K][j-1+K];
      cnt_w_F += sum_w[i-1+K][j-1+K];

      cnt_b_G += sum_b[i-1+K][2*K-1];
      cnt_w_G += sum_w[i-1+K][2*K-1];

      cnt_b_H += sum_b[2*K-1][j-1+K];
      cnt_w_H += sum_w[2*K-1][j-1+K];
      
      cnt_b_I += sum_b[2*K-1][2*K-1];
      cnt_w_I += sum_w[2*K-1][2*K-1];

      // 1.
      tmp_cnt_b += cnt_b_A;
      tmp_cnt_w += cnt_w_A;

      // 2.
      tmp_cnt_b += cnt_b_C - cnt_b_B;
      tmp_cnt_w += cnt_w_C - cnt_w_B;

      // 3.
      tmp_cnt_b += cnt_b_E - cnt_b_D;
      tmp_cnt_w += cnt_w_E - cnt_w_D;

      // 4.
      tmp_cnt_b += cnt_b_F - cnt_b_B - cnt_b_D + cnt_b_A;
      tmp_cnt_w += cnt_w_F - cnt_w_B - cnt_w_D + cnt_w_A;
      
      // 5.
      tmp_cnt_b += cnt_b_I - cnt_b_G - cnt_b_H + cnt_b_F;
      tmp_cnt_w += cnt_w_I - cnt_w_G - cnt_w_H + cnt_w_F;

      chmax(ans, tmp_cnt_b);
      chmax(ans, tmp_cnt_w);

    }
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 126336