#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repn(i,n) for(int i = 0; i <= (int)(n); i++)
#define srep(i,l,n) for(int i = l; i < (int)(n); i++)
#define srepn(i,l,n) for(int i = l; i <= (int)(n); i++)
#define drep(i,n) for(int i = (int)(n-1); i >= 0; i--)
#define drepn(i,n) for(int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var)  do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
template<typename T> inline istream& operator>>(istream &i, vector<T> &v) {rep(j, size(v)) i >> v[j]; return i;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T> T gcd(T a, T b) {if(b==0)return a; else return gcd(b,a%b);}
template<class T> T lcm(T a, T b) {return a/gcd(a,b)*b;}
template<class T = int> using V = vector<T>;
template<class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {return i >= 0 && i < h && j >= 0 && j < w;}
void Yes(){cout << "Yes" << endl;}
void No(){cout << "No" << endl;}
void YES(){cout << "YES" << endl;}
void NO(){cout << "NO" << endl;}
void err() {cout << -1 << endl;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int ddx[8] = {1,1,1,-1,-1,-1,0,0};
int ddy[8] = {0,1,-1,0,1,-1,1,-1};

template <class T>
struct CumlativeSum2 {
    vector<vector<T>> sum;
    CumlativeSum2(int h, int w):sum(h+1, vector<T>(w+1, 0)){};

    // 1-indexedだとそのままの入力でOK
    void add(int x, int y, T z) {
        sum[x][y] += z;
    }

    void build() {
        for(int i = 1; i < size(sum); i++) {
            for(int j = 1; j < size(sum[i]); j++) {
                sum[i][j] += sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1];
            }
        }
    }

    //1-indexだとsx, syの方を-1する必要あり, 0-indexedだとgx, gyの方を+1
    T q(int sx, int sy, int gx, int gy) {
        return sum[gx][gy]-sum[sx][gy]-sum[gx][sy]+sum[sx][sy];
    }
};

struct Data {
    int x, y; char c;
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    auto get = [&](char c, ll n) {
        if(c == 'B') {
            if(n & 1) return 'W';
            else return 'B';
        }else if(c == 'W') {
            if(n & 1) return 'B';
            else return 'W';
        }
    };
    CumlativeSum2<ll> black(k,k), white(k,k);
    rep(i, n) {
        ll x, y; char c;
        cin >> x >> y >> c;
        ll p = x / k + y / k;
        if(get(c, p) == 'B') {
            black.add(x%k+1, y%k+1, 1);
        }else{
            white.add(x%k+1, y%k+1, 1);
        }
    }
    black.build();
    white.build();
    ll ans = 0;
    ll foo;
    repn(i, k) repn(j, k) {
        int now = white.q(0, 0, k, k);
        foo = now - white.q(0, 0, i, j) + black.q(0, 0, i, j) - white.q(i, j, k, k) + black.q(i, j, k, k);
        chmax(ans, max(n - foo, foo));
        foo = now - white.q(i, 0, k, j) + black.q(i, 0, k, j) - white.q(0, j, i, k) + black.q(0, j, i, k);
        chmax(ans, max(n - foo, foo));;
    }
    cout << ans << endl;
}

Measured memory_bytes: 19052