#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
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;
}
struct Edge {
  long long to, cap, rev;
  Edge(long long _to, long long _cap, long long _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
void add_edge(vector<vector<Edge>>& G, long long from, long long to,
              long long cap, bool revFlag, long long revCap) {
  G[from].push_back(Edge(to, cap, (long long)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (long long)G[from].size() - 1));
}
long long max_flow_dfs(vector<vector<Edge>>& G, long long v, long long t,
                       long long f, vector<bool>& used) {
  if (v == t) return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); ++i) {
    Edge& e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      long long d = max_flow_dfs(G, e.to, t, min(f, e.cap), used);
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}
long long max_flow(vector<vector<Edge>>& G, long long s, long long t) {
  long long flow = 0;
  for (;;) {
    vector<bool> used(G.size());
    for (int(i) = 0; (i) < (used.size()); (i)++) used[i] = false;
    long long f = max_flow_dfs(G, s, t, INF, used);
    if (f == 0) {
      return flow;
    }
    flow += f;
  }
}
void BellmanFord(vector<vector<Edge>>& G, long long s, vector<long long>& d,
                 vector<long long>& negative) {
  d.resize(G.size());
  negative.resize(G.size());
  for (int(i) = 0; (i) < (d.size()); (i)++) d[i] = INF;
  for (int(i) = 0; (i) < (d.size()); (i)++) negative[i] = false;
  d[s] = 0;
  for (int(k) = 0; (k) < (G.size() - 2); (k)++) {
    for (int(i) = 0; (i) < (G.size()); (i)++) {
      for (int(j) = 0; (j) < (G[i].size()); (j)++) {
        if (d[G[i][j].to] > d[i] + G[i][j].cap) {
          d[G[i][j].to] = d[i] + G[i][j].cap;
        }
      }
    }
  }
  for (int(k) = 0; (k) < (G.size() - 2); (k)++) {
    for (int(i) = 0; (i) < (G.size()); (i)++) {
      for (int(j) = 0; (j) < (G[i].size()); (j)++) {
        if (d[G[i][j].to] > d[i] + G[i][j].cap) {
          d[G[i][j].to] = d[i] + G[i][j].cap;
          negative[G[i][j].to] = true;
        }
        if (negative[i] == true) negative[G[i][j].to] = true;
      }
    }
  }
}
void Dijkstra(vector<vector<Edge>>& G, long long s, vector<long long>& d) {
  d.resize(G.size());
  for (int(i) = 0; (i) < (d.size()); (i)++) d[i] = INF;
  d[s] = 0;
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      q;
  q.push(make_pair(0, s));
  while (!q.empty()) {
    pair<long long, long long> a = q.top();
    q.pop();
    if (d[a.second] < a.first) continue;
    for (int(i) = 0; (i) < (G[a.second].size()); (i)++) {
      Edge e = G[a.second][i];
      if (d[e.to] > d[a.second] + e.cap) {
        d[e.to] = d[a.second] + e.cap;
        q.push(make_pair(d[e.to], e.to));
      }
    }
  }
}
void WarshallFloyd(vector<vector<Edge>>& G, vector<vector<long long>>& d) {
  d.resize(G.size());
  for (int(i) = 0; (i) < (d.size()); (i)++) d[i].resize(G.size());
  for (int(i) = 0; (i) < (d.size()); (i)++) {
    for (int(j) = 0; (j) < (d[i].size()); (j)++) {
      d[i][j] = INF;
    }
  }
  for (int(i) = 0; (i) < (G.size()); (i)++) {
    d[i][i] = 0;
  }
  for (int(i) = 0; (i) < (G.size()); (i)++) {
    for (int(j) = 0; (j) < (G[i].size()); (j)++) {
      d[i][G[i][j].to] = G[i][j].cap;
    }
  }
  for (int(i) = 0; (i) < (G.size()); (i)++) {
    for (int(j) = 0; (j) < (G.size()); (j)++) {
      for (int(k) = 0; (k) < (G.size()); (k)++) {
        chmin(d[j][k], d[j][i] + d[i][k]);
      }
    }
  }
}
class UnionFind {
  vector<int> data;

 public:
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
void divisor(long long n, vector<long long>& ret) {
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  sort(ret.begin(), ret.end());
}
class Combination {
 public:
  vector<long long> fact;
  vector<long long> inv;
  long long mod;
  long long mod_inv(long long x) {
    long long n = mod - 2;
    long long res = 1LL;
    while (n > 0) {
      if (n & 1) res = res * x % mod;
      x = x * x % mod;
      n >>= 1;
    }
    return res;
  }
  long long nCr(long long n, long long r) {
    return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod;
  }
  long long nPr(long long n, long long r) {
    return (fact[n] * inv[n - r]) % mod;
  }
  Combination(long long n, long long _mod) {
    mod = _mod;
    fact.resize(n + 1);
    fact[0] = 1;
    for (int(i) = 0; (i) < (n); (i)++) {
      fact[i + 1] = (fact[i] * (i + 1LL)) % mod;
    }
    inv.resize(n + 1);
    for (int(i) = 0; (i) < (n + 1); (i)++) {
      inv[i] = mod_inv(fact[i]);
    }
  }
};
bool compare_by_b(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.second != b.second) {
    return a.second > b.second;
  } else {
    return a.first > b.first;
  }
}
bool compare_by_a(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.first != b.first) {
    return a.first > b.first;
  } else {
    return a.second > b.second;
  }
}
long long gcd(long long m, long long n) {
  if (n == 0) return m;
  return gcd(n, m % n);
}
long long lcm(long long m, long long n) { return m / gcd(m, n) * n; }
long long cnt[2001][2001];
int main() {
  long long n, k;
  cin >> n >> k;
  for (int(i) = 0; (i) < (n); (i)++) {
    long long x, y;
    cin >> x >> y;
    char c;
    cin >> c;
    if (c == 'W') {
      x += k;
    }
    x %= 2 * k;
    y %= 2 * k;
    cnt[x + 1][y + 1]++;
  }
  for (int(i) = 0; (i) < (2001); (i)++) {
    for (int(j) = 0; (j) < (2000); (j)++) cnt[i][j + 1] += cnt[i][j];
  }
  for (int(i) = 0; (i) < (2000); (i)++) {
    for (int(j) = 0; (j) < (2001); (j)++) cnt[i + 1][j] += cnt[i][j];
  }
  long long max = 0;
  for (int(i) = 0; (i) < (k + 1); (i)++) {
    for (int(j) = 0; (j) < (k + 1); (j)++) {
      long long bk = 0;
      bk += cnt[i][j];
      bk += cnt[i][2 * k] - cnt[i][j + k];
      bk += cnt[2 * k][j] - cnt[i + k][j];
      bk += cnt[i + k][j + k] - cnt[i][j + k] - cnt[i + k][j] + cnt[i][j];
      bk += cnt[2 * k][2 * k] - cnt[2 * k][j + k] - cnt[i + k][2 * k] +
            cnt[i + k][j + k];
      chmax(max, bk);
    }
  }
  cout << max;
  return 0;
}
