#include <bits/stdc++.h>
#define ll long long
#define rep(X,N) for(int X = 0; X < (N); X++)
#define PI (acos(-1.0))
#define pback push_back
#define mpair make_pair
#define MODN 1000000007
#define ALL(V) (V).begin(),(V).end() 
#define CERR if(false) cerr

using namespace std;

int main(){

    int n,k,x,y;
    char c;
    cin >> n >> k;

    vector<vector<int>> black(k * 2, vector<int>(k * 2));
    vector<vector<int>> white(k * 2, vector<int>(k * 2));
    
    vector<vector<int>> black_imos(k + 1, vector<int>(k + 1));
    vector<vector<int>> white_imos(k + 1, vector<int>(k + 1));

    rep(i,n){
        cin >> x >> y >> c;

        if(c == 'B'){
            black[y % (2 * k)][x % (2 * k)]++;
        }else{
            white[y % (2 * k)][x % (2 * k)]++;
        }
    }

    rep(i,k){
        rep(j,k){
            black[i][j] += black[k + i][k + j];
            white[i][j] += white[k + i][k + j];
        }
    }

    rep(i,k){
        rep(j,k){
            black[i + k][j] += black[i][k + j];
            white[i + k][j] += white[i][k + j];
        }
    }

    rep(i,k){
        rep(j,k){
            black[i][j] += white[i + k][j];
            white[i][j] += black[i + k][j];
        }
    }

    rep(i,k){
        rep(j,k){
            black_imos[i][j] += black[i][j];
            black_imos[i][k] += -1 * black[i][j];
            black_imos[k][j] += -1 * black[i][j];
            black_imos[k][k] += black[i][j];

            white_imos[i][j] += white[i][j];
            white_imos[i][k] += -1 * white[i][j];
            white_imos[k][j] += -1 * white[i][j];
            white_imos[k][k] += white[i][j];
        }
    }

    rep(i,k){
        rep(j,k){
            black_imos[i][j + 1] += black_imos[i][j];
            white_imos[i][j + 1] += white_imos[i][j];
        }
    }

    rep(i,k){
        rep(j,k){
            black_imos[i + 1][j] += black_imos[i][j];
            white_imos[i + 1][j] += white_imos[i][j];
        }
    }

    int ans = 0;

    //cerr << blacksum << " " << whitesum << endl;

    rep(i,k){
        rep(j,k){
            int tmp = white_imos[i][j] + black_imos[k - 1][k - 1] - black_imos[i][j];
            if(tmp > ans){
                ans = tmp;
                //cerr << "white:" << i << " " << j << endl;
            }
        }
    }

    ans = max(ans, black_imos[k - 1][k - 1]);

    rep(i,k){
        rep(j,k){
            int tmp = black_imos[i][j] + white_imos[k - 1][k - 1]- white_imos[i][j];
            if(tmp > ans){
                ans = tmp;
                //cerr << "black:" << i << " " << j << endl;
            }
        }
    }

    ans = max(ans, white_imos[k - 1][k - 1]);


    if(false){

        int tmp = min(10,k + 1);

        rep(i, tmp){
            rep(j, tmp){
                cerr << white_imos[i][j] << " ";
            }
            cerr << endl;
        }

        cerr << endl;

        rep(i, tmp){
            rep(j,tmp){
                cerr << black_imos[i][j] << " ";
            }
            cerr << endl;
        }
    }

    cout << ans << endl;


    return 0; 
}