#include <bits/stdc++.h>
using namespace std;
using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;

const ll MOD = (ll)1e9 + 7;
const ll HIGHINF = (ll)1e18;

int main() {
  ll n, k; cin >> n >> k;
  vvll checker(k+1, vll(k+1, 0));
  ll x, y; char c; bool is_b;
  for (ll i=0; i<n; i++) {
    cin >> x >> y >> c;
    x %= 2*k; y %= 2*k;
    is_b = c == 'B';
    if (x >= k) {x -= k; is_b=!is_b;}
    if (y >= k) {y -= k; is_b=!is_b;}
    if (is_b) {
      checker.at(0).at(0)++;
      checker.at(y+1).at(0)--;
      checker.at(0).at(x+1)--;
      checker.at(y+1).at(x+1)+=2;
    } else {
      checker.at(y+1).at(0)++;
      checker.at(0).at(x+1)++;
      checker.at(y+1).at(x+1)-=2;
    }
  }
  for (ll i=1; i<k; i++) {
    for (ll j=0; j<k+1; j++) {
      checker.at(j).at(i) += checker.at(j).at(i-1);
    }
  }
  for (ll i=1; i<k; i++) {
    for (ll j=0; j<k+1; j++) {
      checker.at(i).at(j) += checker.at(i-1).at(j);
    }
  }
  ll ans = 0;
  for (ll i=0; i<k; i++) for (ll j=0; j<k; j++) {
    ans = max(ans, max(checker.at(i).at(j), n-checker.at(i).at(j)));
  }
  cout << ans << endl;
  return 0;
}
