#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <cmath>
#include <numeric>
#include <iomanip>

using namespace std;
int cumsum_white[2000][2000];
int cumsum_black[2000][2000];
int count_color_box(int s_r, int s_c, int e_r, int e_c, char color){
  if(color == 'W'){
    if(s_r == 0 && s_c == 0){
      return cumsum_white[e_r][e_c];
    } else if(s_c == 0){
      return cumsum_white[e_r][e_c] - cumsum_white[s_r - 1][e_c];
    } else if(s_r == 0){
      return cumsum_white[e_r][e_c] - cumsum_white[e_r][s_c - 1];
    } else {
      return cumsum_white[e_r][e_c]
        - cumsum_white[s_r - 1][e_c] - cumsum_white[e_r][s_c - 1] + cumsum_white[s_r - 1][s_c - 1];
    }
  } else {
    if(s_r == 0 && s_c == 0){
      return cumsum_black[e_r][e_c];
    } else if(s_c == 0){
      return cumsum_black[e_r][e_c] - cumsum_black[s_r - 1][e_c];
    } else if(s_r == 0){
      return cumsum_black[e_r][e_c] - cumsum_black[e_r][s_c - 1];
    } else {
      return cumsum_black[e_r][e_c]
        - cumsum_black[s_r - 1][e_c] - cumsum_black[e_r][s_c - 1] + cumsum_black[s_r - 1][s_c - 1];
    }
  }
  //error
  return INT_MIN;
}

int count_color(int s_r, int s_c, char color, int K){
  s_r = (s_r % (2 * K));
  s_c = (s_c % (2 * K));
  if(s_r + K - 1 < 2 * K && s_c + K - 1 < 2 * K){
    return count_color_box(s_r, s_c, s_r + K - 1, s_c + K - 1, color);
  } else if(s_r + K - 1 < 2 * K){
      return count_color_box(s_r, s_c, s_r + K - 1, 2 * K - 1, color)
        + count_color_box(s_r, 0, s_r + K - 1, s_c - K - 1, color);
  } else if(s_c + K - 1 < 2 * K){
      return count_color_box(s_r, s_c, 2 * K - 1, s_c + K - 1, color)
        + count_color_box(0, s_c, s_r - K - 1, s_c + K - 1, color);
  } else {
      return count_color_box(s_r, s_c, 2 * K - 1, 2 * K - 1, color) +
        + count_color_box(0, s_c, s_r - K - 1, 2 * K - 1, color)
        + count_color_box(s_r, 0, 2 * K - 1, s_c - K - 1, color)
        + count_color_box(0, 0, s_r - K - 1, s_c - K - 1, color);
  }
  //error
  return INT_MIN;
}

int main(){
    int N;
    int K;
    cin >> N >> K;
    for(int i = 0; i < N; i++){
      int x;
      int y;
      char c;
      cin >> x >> y >> c;
      if(c == 'W'){
        cumsum_white[x % (2 * K)][y % (2 * K)] ++;
      } else {
        cumsum_black[x % (2 * K)][y % (2 * K)] ++;
      }
    }

    for(int i = 0; i < 2 * K; i++){
      for(int j = 1; j < 2 * K; j++){
        cumsum_white[i][j] = cumsum_white[i][j] + cumsum_white[i][j - 1];
        cumsum_black[i][j] = cumsum_black[i][j] + cumsum_black[i][j - 1];
      }
    }

    for(int i = 0; i < 2 * K; i++){
      for(int j = 1; j < 2 * K; j++){
        cumsum_white[j][i] = cumsum_white[j][i] + cumsum_white[j - 1][i];
        cumsum_black[j][i] = cumsum_black[j][i] + cumsum_black[j - 1][i];
      }
    }

    int ans = 0;
    for(int i = 0; i < 2 * K; i++){
      for(int j = 0; j < 2 * K; j++){
        int tmp_ans = count_color(i, j, 'B', K) + count_color(i + K, j + K, 'B', K)
          + count_color(i + K, j, 'W', K) + count_color(i, j + K, 'W', K);
        ans = max(ans, tmp_ans);
      }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 31488