//g++ -std=c++14 test.cpp -o test.out
//問題URL
//https://atcoder.jp/contests/abc086/tasks/arc089_b

//参考：
//http://xxxasdfghjk999.hatenablog.jp/entry/2018/08/19/034403
// ポイント1：散らばっている情報を、決められた箇所に押し込む
// ポイント2：二次元累積和。DPっぽくすれば求められる
// ポイント3：繰り返し構造がある時は周期性に注目してみる

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <map> 
#include <unordered_map> 
#include <utility>
#include <limits.h>
#include <bitset>
#include <set>
using namespace std;
 
#define LL long long int
const LL INF = (1LL<<60);
const int INF_INT = 2147483647-1e6-1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N,K;
LL X[100000],Y[100000];
char C[100000];

LL ans = 0;

void solve(){
    vector<vector<LL>> accum(2*K,vector<LL>(2*K,0));
    for(int i=0;i<N;i++){
        X[i] %= (2*K);
        Y[i] %= (2*K);
        if(C[i]=='B') X[i] = (X[i]+K)%(2*K);
        accum[X[i]][Y[i]]++;
    }

    for(int y=1;y<2*K;y++) accum[0][y] += accum[0][y-1];
    for(int x=1;x<2*K;x++) accum[x][0] += accum[x-1][0];
    for(int y=1;y<2*K;y++){
        for(int x=1;x<2*K;x++){
            accum[x][y] += (accum[x-1][y]+accum[x][y-1]-accum[x-1][y-1]);
        }
    }

    auto get_accum = [&](int start_x,int start_y,int end_x,int end_y){
        LL ret = accum[end_x][end_y];
        if(start_x>=1 && start_y>=1){
            ret -= accum[start_x-1][end_y];
            ret -= accum[end_x][start_y-1];
            ret += accum[start_x-1][start_y-1];
        }else if(start_x>=1){
            ret -= accum[start_x-1][end_y];
        }else if(start_y>=1){
            ret -= accum[end_x][start_y-1];
        }
        return ret;
    };

    auto get_accum_rec = [&](int start_x,int start_y,int end_x,int end_y){
        if(start_x<=end_x && start_y<=end_y) return get_accum(start_x,start_y,end_x,end_y);
        if(start_x>end_x && start_y<=end_y) return get_accum(start_x,start_y,2*K-1,end_y)+get_accum(0,start_y,end_x,end_y);
        if(start_x<=end_x && start_y>end_y) return get_accum(start_x,start_y,end_x,2*K-1)+get_accum(start_x,0,end_x,end_y);
        LL ret = 0;
        ret += get_accum(start_x,start_y,2*K-1,2*K-1);
        ret += get_accum(0,0,end_x,end_y);
        ret += get_accum(0,start_y,end_x,2*K-1);
        ret += get_accum(start_x,0,2*K-1,end_y);
        return ret;
    };

    for(int start_y=0;start_y<2*K;start_y++){
        for(int start_x=0;start_x<2*K;start_x++){
            LL middle_x = (start_x+K-1)%(2*K);
            LL middle_y = (start_y+K-1)%(2*K);
            LL next_x = (middle_x+1)%(2*K);
            LL next_y = (middle_y+1)%(2*K);
            LL end_x = (next_x+K-1)%(2*K);
            LL end_y = (next_y+K-1)%(2*K);
            LL num = 0;
            num += get_accum_rec(start_x,start_y,middle_x,middle_y);
            num += get_accum_rec(next_x,next_y,end_x,end_y);
            ans = max(ans,num);
            ans = max(ans,N-num);
        }
    }
}

int main(){
    cin >> N >> K;
    for(int i=0;i<N;i++) cin >> X[i] >> Y[i] >> C[i];

    solve();

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 35876