#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.tie(0);
//    ios::sync_with_stdio(false);
//
//    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));
//    int white_num = 0;
//    int black_num = 0;
//    rep(i, N) {
//        int 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++;
//        }
//    }
//    v2int Sb(2 * K + 1, vint(2 * K + 1, 0));
//    v2int Sw(2 * K + 1, vint(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];
//        }
//    }
//
//    int count_b, count_w;
//    int 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];
//            //}
//
//            int score1 = count_b + (white_num - count_w);
//            int 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;
    v2int field_black(K, vint(2 * K, 0));
    v2int field_white(K, vint(2 * K, 0));
    int white_num = 0; 
    int black_num = 0;
    rep(i, N) {
        int c, r;
        char t;
        cin >> c >> r >> t;
     
        c = (c) % (2 * K);
        r = (r) % (2 * K);
        if (c < K && r < K) {
           
        }
        else if (c < K && r >= K) {
            r = r - K;
            c = c + K;
        }
        else if (c >= K && r < K) {
          
        }
        else {
            r = r - K;
            c = c - K;
        }

        if (t == 'W') {
            field_white[r][c]++;
            white_num++;
        }
        else {
            field_black[r][c]++;
            black_num++;
        }
    }
    v2int Sb(K + 1, vint(2 * K + 1, 0));
    v2int Sw(K + 1, vint(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];
        }
    }

    int count_b, count_w;
    int 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];

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

Measured memory_bytes: 31616