#include <iostream>
#include <vector>
using namespace std;

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

  int N, K; cin >> N >> K;
  int K2 = K * 2;
  
  vector<vector<int>> a(K2, vector<int>(K2));
  for (int i = 0; i < N; i++) {
    int x, y; cin >> x >> y;
    char c; cin >> c;
    if (c == 'W') x += K;
    a[x%K2][y%K2]++;
  }

  vector<vector<int>> cs(K2+1, vector<int>(K2+1));
  for (int i = 0; i < K2; i++) {
    for (int j = 0; j < K2; j++) {
      cs[i+1][j+1] = cs[i][j+1] + cs[i+1][j]
                   - cs[i][j] + a[i][j];
    }
  }

  auto query = [&](int x1, int y1, int x2, int y2) {
    return cs[x2][y2] - cs[x1][y2] - cs[x2][y1] + cs[x1][y1];
  };

  int ans = 0;
  for (int i = 0; i <= K; i++) {
    for (int j = 0; j <= K; j++) {
      int res = query(0, 0, i, j)
              + query(i, j, i+K, j+K)
              + query(0, j+K, i, K2)
              + query(i+K, 0, K2, j)
              + query(i+K, j+K, K2, K2);
      ans = max(ans, res);
      ans = max(ans, N-res);
    }
  }
  cout << ans << "\n";
}