#include <stdio.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef list<int> liint;
typedef pair<int, int> pint;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b) { a > b ? : a = b; }
template<class T, class C> void chmin(T& a, C b) { a < b ? : a = b; }


int main() {
    int N, K;
    cin >> N >> K;
    //vint R(N), C(N);
    //vector<char> T(N);
    v2int field_black(2 * K, vint(2 * K, 0));
    v2int field_white(2 * K, vint(2 * K, 0));
    ll white_num = 0;
    ll black_num = 0;
    rep(i, N) {
        ll c, r;
        char t;
        cin >> c >> r >> t;

        c = (c) % (2 * K);
        r = (r) % (2 * K);

        if (t == 'W') {
            field_white[r][c]++;
            white_num++;
        }
        else {
            field_black[r][c]++;
            black_num++;
        }
    }
    v2ll Sb(2 * K + 1, vll(2 * K + 1, 0));
    v2ll Sw(2 * K + 1, vll(2 * K + 1, 0));

    REP(i, 1, 2 * K + 1) {
        REP(j, 1, 2 * K + 1) {
            Sb[i][j] = Sb[i - 1][j] + Sb[i][j - 1] - Sb[i - 1][j - 1] + field_black[i - 1][j - 1];
            Sw[i][j] = Sw[i - 1][j] + Sw[i][j - 1] - Sw[i - 1][j - 1] + field_white[i - 1][j - 1];
        }
    }

    ll count_b, count_w;
    ll max_score = 0;
    REP(i, 1, K + 2) {
        REP(j, 1, K + 2) {
            int Sr1 = i;
            int Sc1 = j;
            int Er1 = i + K - 1;
            int Ec1 = j + K - 1;
            count_b = Sb[Er1][Ec1] - Sb[Sr1 - 1][Ec1] - Sb[Er1][Sc1 - 1] + Sb[Sr1 - 1][Sc1 - 1];
            count_w = Sw[Er1][Ec1] - Sw[Sr1 - 1][Ec1] - Sw[Er1][Sc1 - 1] + Sw[Sr1 - 1][Sc1 - 1];


            int Sr2 = i + K;
            int Sc2 = j + K;
            int Er2 = 2 * K;
            int Ec2 = 2 * K;
            count_b += Sb[Er2][Ec2] - Sb[Sr2 - 1][Ec2] - Sb[Er2][Sc2 - 1] + Sb[Sr2 - 1][Sc2 - 1];
            count_w += Sw[Er2][Ec2] - Sw[Sr2 - 1][Ec2] - Sw[Er2][Sc2 - 1] + Sw[Sr2 - 1][Sc2 - 1];

            int Sr3 = 1;
            int Sc3 = 1;
            int Er3 = Sr1 - 1;
            int Ec3 = Sc1 - 1;
            //if (Ec3 >= Sc3 && Er3 >= Sr3) {
                count_b += Sb[Er3][Ec3] - Sb[Sr3 - 1][Ec3] - Sb[Er3][Sc3 - 1] + Sb[Sr3 - 1][Sc3 - 1];
                count_w += Sw[Er3][Ec3] - Sw[Sr3 - 1][Ec3] - Sw[Er3][Sc3 - 1] + Sw[Sr3 - 1][Sc3 - 1];
            //}

            int Sr4 = 1;
            int Sc4 = Sc2;
            int Er4 = Sr1 - 1;
            int Ec4 = 2 * K;
            //if (Ec4 >= Sc4 && Er4 >= Sr4) {
                count_b += Sb[Er4][Ec4] - Sb[Sr4 - 1][Ec4] - Sb[Er4][Sc4 - 1] + Sb[Sr4 - 1][Sc4 - 1];
                count_w += Sw[Er4][Ec4] - Sw[Sr4 - 1][Ec4] - Sw[Er4][Sc4 - 1] + Sw[Sr4 - 1][Sc4 - 1];
            //}

            int Sr5 = Sr2;
            int Sc5 = 1;
            int Er5 = 2 * K;
            int Ec5 = Sc1 - 1;
            //if (Ec5 >= Sc5 && Er5 >= Sr5) {
                count_b += Sb[Er5][Ec5] - Sb[Sr5 - 1][Ec5] - Sb[Er5][Sc5 - 1] + Sb[Sr5 - 1][Sc5 - 1];
                count_w += Sw[Er5][Ec5] - Sw[Sr5 - 1][Ec5] - Sw[Er5][Sc5 - 1] + Sw[Sr5 - 1][Sc5 - 1];
            //}

            ll score1 = count_b + (white_num - count_w);
            ll score2 = count_w + (black_num - count_b);
            max_score = max(max(max_score, score1), score2);
        }
    }
    cout << max_score << endl;
    return 0;
}

//int main() {
//    int N, K;
//    cin >> N >> K;
//    vint R(N), C(N);
//    vector<char> T(N);
//    v2int field_black(K, vint(2 * K, 0));
//    v2int field_white(K, vint(2 * K, 0));
//    ll white_num = 0; 
//    rep(i, N) {
//        cin >> C[i] >> R[i] >> T[i];
//     
//        int c = (C[i]) % (2 * K);
//        int r = (R[i]) % (2 * K);
//        if (c < K && r < K) {
//            C[i] = c;
//            R[i] = r;
//        }
//        else if (c < K && r >= K) {
//            R[i] = r - K;
//            C[i] = c + K;
//        }
//        else if (c >= K && r < K) {
//            R[i] = r ;
//            C[i] = c ;
//        }
//        else {
//            R[i] = r - K;
//            C[i] = c - K;
//        }
//
//        if (T[i] == 'W') {
//            field_white[R[i]][C[i]]++;
//            white_num++;
//        }
//        else {
//            field_black[R[i]][C[i]]++;
//        }
//    }
//    v2ll Sb(K + 1, vll(2 * K + 1, 0));
//    v2ll Sw(K + 1, vll(2 * K + 1, 0));
//
//    REP(i, 1,  K + 1) {
//        REP(j, 1, 2 * K + 1) {
//            Sb[i][j] = Sb[i - 1][j] + Sb[i][j - 1] - Sb[i - 1][j - 1] + field_black[i - 1][j - 1];
//            Sw[i][j] = Sw[i - 1][j] + Sw[i][j - 1] - Sw[i - 1][j - 1] + field_white[i - 1][j - 1];
//        }
//    }
//
//    ll count_b, count_w;
//    ll max_score = 0;
//    REP(i, 1, K + 2) {
//        REP(j, 1, K + 2) {
//            int Or = i - 1;
//            int Er = min(i + K - 1, K);
//            int Ec = min(j + K - 1, 2 * K);
//            count_b = Sb[Er][Ec] - Sb[i - 1][Ec] - Sb[Er][j - 1] + Sb[i - 1][j - 1];
//            count_w = Sw[Er][Ec] - Sw[i - 1][Ec] - Sw[Er][j - 1] + Sw[i - 1][j - 1];
//            count_b += Sb[Or][j - 1];
//            count_w += Sw[Or][j - 1];
//            count_b += Sb[Or][2 * K] - Sb[0][2 * K] - Sb[Or][K + j - 1] + Sb[0][K + j - 1];
//            count_w += Sw[Or][2 * K] - Sw[0][2 * K] - Sw[Or][K + j - 1] + Sw[0][K + j - 1];
//
//            ll score = count_b + (white_num - count_w);
//            max_score = max(max_score, score);
//        }
//    }
//    cout << max_score << endl;
//    return 0;
//}

Measured memory_bytes: 94336