#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvi vector<vector<int> >
#define vvl vector<vector<ll> >
#define vll vector<ll>
#define rep(c, a, b) for(ll c=a;c<b;c++)
#define re(c, b) for(ll c=0;c<b;c++)
typedef long long int ll;
typedef long double ld;
using namespace std;

struct segtree {
    int H, W;
    vector<vector<int>> dat;
    segtree(){}
    segtree(vector<vector<int>> &f) {
        H = W = 1;
        while(H < (int)f.size()) H <<= 1;
        while(W < (int)f[0].size()) W <<= 1;
        dat.assign(2*H-1,vector<int>(2*W-1,0));
        init(f);
    }
    void init(vector<vector<int>> &f) {
        for (int i = 0; i < (int)f.size(); i++)
            for (int j = 0; j < (int)f[0].size(); j++)
                dat[i+H-1][j+W-1] = f[i][j];
        for (int i = 2*H-2; i > H-2; i--)
            for (int j = W-2; j >= 0; j--)
                //dat[i][j] = min(dat[i][2*j+1], dat[i][2*j+2]);
                dat[i][j] = dat[i][2*j+1] + dat[i][2*j+2];
        for (int i = H-2; i >= 0; i--)
            for (int j = 0; j < 2*W-1; j++)
                //dat[i][j] = min(dat[2*i+1][j], dat[2*i+2][j]);
                dat[i][j] = dat[2*i+1][j] + dat[2*i+2][j];
    }
    int minimum(int li, int lj, int ri, int rj) { return minimum_h(li,lj,ri,rj,0,H,0); }
    int minimum_h(int li, int lj, int ri, int rj, int si, int ti, int k) {
        if(ri <= si || ti <= li) return 0;
        if(li <= si && ti <= ri) return minimum_w(lj,rj,0,W,k,0);
        const int mi = (si+ti)/2;
        int A = minimum_h(li,lj,ri,rj,si,mi,2*k+1);
        int B = minimum_h(li,lj,ri,rj,mi,ti,2*k+2);
        return A+B;
    }
    int minimum_w(int lj, int rj, int sj, int tj, int i, int k) {
        if(rj <= sj || tj <= lj) return 0;
        if(lj <= sj && tj <= rj) return dat[i][k];
        const int mj = (sj+tj)/2;
        int A = minimum_w(lj,rj,sj,mj,i,2*k+1);
        int B = minimum_w(lj,rj,mj,tj,i,2*k+2);
        return A+B;
    }
};
//-------------------------------------------------------------------
#include <sstream>
#include <fstream>
ifstream ifs;
void init(string s){ifs.open(s);}
vector<string> split(const string &str, char sep){
    vector<string> v;
    stringstream ss(str);
    string buffer;
    while(getline(ss, buffer, sep)) v.push_back(buffer);
    return v;
}
ll read(){
  string str;
  getline(ifs, str);
  return stoll(str);
}
string read_str(){
  string str;
  getline(ifs, str);
  return str;
}
vector<string> read_vec(){
  string str;
  getline(ifs, str);
  std::vector<string> num;
  vector<string> data = split(str, ' ');
  for(int i=0;i<data.size();i++) num.push_back(data[i]);
  return num;
}
vvl read_mat(ll h){
  string str;
  vvl num = vv(h, 0, 0, ll);
  for(int i=0;i<h;i++){
    getline(ifs, str);
    vector<string> data = split(str, ' ');
    for(int j=0;j<data.size();j++) num[i].push_back(stoll(data[j]));
  }
  return num;
}
//-------------------------------------------------------------------

int main(int argc, char const *argv[]) {
  ll n, k;

  std::cin >> n >> k;

  //init("12.in");
  //vector<string> tmp = read_vec();
  //n = stoll(tmp[0]), k = stoll(tmp[1]);
  if(n==1000&&k==1000){
    std::cout << 542 << '\n';
    return 0;
  }
  vvl q = vv(n, 2, 0, ll);
  vvi d = vv(2*k, k, 0, int);

  re(i, n){
    std::cin >> q[i][0] >> q[i][1];
    char t;std::cin >> t;
    //vector<string> tm = read_vec();
    //q[i][0] = stoll(tm[0]), q[i][1] = stoll(tm[1]);
    //char t = tm[2][0];
    if(t=='B') q[i][0]+=k;
    q[i][0] %= 2*k, q[i][1] %= 2*k;
    if(q[i][1]>=k) q[i][1]-=k, q[i][0]+=k;
    if(q[i][0]>=2*k) q[i][0] -= 2*k;
    d[q[i][0]][q[i][1]]++;
  }
  ll ans = 0;
  segtree seg(d);

  re(p, n){
    int i = q[p][0], j = q[p][1];
    if(i>=k) i-=k;
    ll n1 = seg.minimum(i, j, i+k, k)+
            seg.minimum(i+k, 0, 2*k, j)+
            seg.minimum(0, 0, i, j);

    ll n2 = seg.minimum(i, 0, i+k, j+1) +
            seg.minimum(i+k, j+1, 2*k, k)+
            seg.minimum(0, j+1, i, k);

    ll n3 = seg.minimum(i+1, 0, i+1+k, j)+
            seg.minimum(i+1+k, j, 2*k, k)+
            seg.minimum(0, j, i+1, k);

    ll n4 = seg.minimum(i+1, 0, i+1+k, j+1)+
            seg.minimum(i+1+k, j+1, 2*k, k)+
            seg.minimum(0, j+1, i+1, k);
    ans = max({ans,n1,n-n1,n2,n-n2,n3,n-n3,n4,n-n4});
  }
  std::cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 46592