#include <bits/stdc++.h>
using namespace std;

int n, k;


int arr[2001][2001];
int arr_doubled[4002][4002];
int get(int y, int x){ 
  return arr_doubled[y+k-1][x+k-1]+arr_doubled[y-1][x-1]-arr_doubled[y-1][x+k-1]-arr_doubled[y+k-1][x-1];
}
int main() {
  /*int n = 2;
  int m = 4;
  int sum = 0;
  for (int i=1; i<=n; i++){
    int x = m*(m+1)/2;
    sum += n*m*m-n*x+m*n-i*m*m+i*x-m*i+m*m-x+m;
  }
  cout << sum;*/
  cin >> n >> k;
 
  for (int i=0; i<n; i++){
    int x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'W'){
      x += k;
    }
   
    arr[y%(2*k)][x%(2*k)]++;
  }
  
  for (int i=1; i<=4*k; i++){
    for (int j=1; j<=4*k; j++){
      arr_doubled[i][j] = arr_doubled[i][j-1]+arr[(i-1)%(2*k)][(j-1)%(2*k)];
    }
  }

 
  for (int i=1; i<=4*k; i++){
    for (int j=2; j<=4*k; j++){
      arr_doubled[j][i] += arr_doubled[j-1][i];
    }
  }
  int biggest = -1;
  for (int i=1; i<=2*k; i++){
    for (int j=1; j<=2*k; j++){
      biggest = max(get(i, j)+get(i+k, j+k), biggest);
      
    }
  }
  cout << biggest;
  return 0;
}