#include <bits/stdc++.h>
using namespace std;

#define times(n, i)      uptil(0, n, i)
#define rtimes(n, i)     downto((n) - 1, 0, i)
#define upto(f, t, i)    for(decltype(t) i##0_to = (t), i = (f); i <= i##0_to; i++)
#define uptil(f, t, i)   for(decltype(t) i##0_to = (t), i = (f); i <  i##0_to; i++)
#define downto(f, t, i)  for(decltype(f) i##0_to = (t), i = (f); i >= i##0_to; i--)
#define downtil(f, t, i) for(decltype(f) i##0_to = (t), i = (f); i >  i##0_to; i--)
// types
    typedef long double LD;
    #define long long long
    //#define int long
    using VI = vector<int>;
    using WI = vector<VI>;
    using PI = pair<int, int>;
    using VPI = vector<PI>;
    using WPI = vector<VPI>;

bool debug;
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 2
#define _LIBCPP_DEBUG2 2
#define ln << '\n'
#define tb << '\t'
#define sp << ' '
#define db(x) if(debug) cerr << #x << " = " << (x) << ", "
#define dbg(x) if(debug) cerr << #x << " = " << (x) ln

constexpr long INF = 1LL << 60;

void solve();

signed main(signed argc, char *argv[]) {
    #ifdef EBUG
        debug = true;
    #elif defined(ONLINE_JUDGE)
        debug = false;
    #else
        debug = argc >= 2;
    #endif
    if(!debug) {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(20);

    solve();

    return 0;
}

/******************************* basic library ********************************/
// IO
    template<class T>
    istream& operator>>(istream& s, vector<T>& v) {
        for(auto&& p : v) s >> p; return s;
    }
    template<class T>
    ostream& operator<<(ostream& s, vector<T> v) {
        int i = 0;
        for(const auto& p : v) s << (i++?" ":"") << p;
        return s;
    }
    template<class T>
    ostream& operator<<(ostream& s, vector<vector<T>> w) {
        for(const auto& v : w) {
            int i = 0;
            for(const auto& p : v) s << (i++?" ":"") << p;
            s ln;
        }
        return s;
    }
    template<class T, class... A>
    T RD(A... a) { T t(a...); cin >> t; return t; }
// other
    #define all(x) (x).begin(), (x).end()
    #define b_max(x, y) x = max(x, y)
    #define b_min(x, y) x = min(x, y)
    inline LD AC(LD d) { return d ? d : 0; }

/****************************** optional library ******************************/
//-mod constexpr long MOD = 1000000009; // 1000000007; // 998244353;

/************************************ main ************************************/

void solve() {
    int N, K; scanf("%d%d", &N, &K);
    int *X = new int[N], *Y = new int[N];
    char *C = new char[N];
    times(N, i) scanf("%d%d %c", &X[i], &Y[i], &C[i]);

    int L = K * 2, ans = 0, *diff = new int[L];
    times(N, i) Y[i] %= L;
    times(L, j) {
        int a = 0;
        times(L, k) diff[k] = 0;
        times(N, i) {
            bool x = (X[i] + j) % L < K,
                 y = Y[i] < K,
                 z = C[i] == 'B';
            a += (x == y) == z;
            diff[Y[i]] += (x == z) ? 1 : -1;
        }
        int g = K;
        times(L, k) {
            ans = max(ans, a);
            a += diff[g] - diff[k];
            ++g; if(g == L) g = 0;
        }
    }
    printf("%d\n", ans);
}


Measured memory_bytes: 1152