#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}


int main() {
    int n,k;
    cin >> n >> k;

    int a[2*k][2*k][2];
    rep(i,2*k){
        rep(j,2*k){
            a[i][j][0] = 0;
            a[i][j][1] = 0;
        }
    }
    rep(i,n){
        int x,y;
        cin >> x >> y;
        char c;
        cin >> c;
        if(c=='B'){
            a[x%(2*k)][y%(2*k)][0]++;
        }else{
            a[x%(2*k)][y%(2*k)][1]++;
        }
    }
    /*
    rep(i,2*k){
        rep(j,2*k){
            cout << a[i][j][0];
        }
        cout << endl;
    }
    cout << endl;
    rep(i,2*k){
        rep(j,2*k){
            cout << a[i][j][1];
        }
        cout << endl;
    }
    cout << endl;
    */

    int row[2*k][2] = {}; // 0 is true, 1 is false.
    rep(i,2*k){
        rep(j,2*k){
            if((i<k&&j<k)||(k<=i&&k<=j)){
                row[j][0] += a[i][j][0];
                row[j][1] += a[i][j][1];
            }else{
                row[j][0] += a[i][j][1];
                row[j][1] += a[i][j][0];
            }
        }
    }


    int dp[2*k][2*k];
    dp[0][0] = 0;
    rep(j,2*k){
        dp[0][0] += row[j][0];
    }

    rep(i,2*k){
        rep(j,2*k){
            if(i==0&&j==0){
                continue;
            }else if(j==0){
                swap(row[2*k-1][0],row[2*k-1][1]);
                swap(row[(j-1+k)%(2*k)][0],row[(j-1+k)%(2*k)][1]);
                dp[i][j] = 0;
                rep(l,2*k){
                    if(l<k){
                        row[l][0] -= a[i-1][l][0];
                        row[l][0] += a[i-1][l][1];
                        row[l][1] += a[i-1][l][0];
                        row[l][1] -= a[i-1][l][1];
                        row[l][0] -= a[(i+k-1)%(2*k)][l][1];
                        row[l][0] += a[(i+k-1)%(2*k)][l][0];
                        row[l][1] += a[(i+k-1)%(2*k)][l][1];
                        row[l][1] -= a[(i+k-1)%(2*k)][l][0];
                    }else{
                        row[l][0] -= a[i-1][l][1];
                        row[l][0] += a[i-1][l][0];
                        row[l][1] += a[i-1][l][1];
                        row[l][1] -= a[i-1][l][0];
                        row[l][0] -= a[(i+k-1)%(2*k)][l][0];
                        row[l][0] += a[(i+k-1)%(2*k)][l][1];
                        row[l][1] += a[(i+k-1)%(2*k)][l][0];
                        row[l][1] -= a[(i+k-1)%(2*k)][l][1];
                    }
                        
                    dp[i][j] += row[l][0];
                }
            }else{
                dp[i][j] = dp[i][j-1];
                dp[i][j] -= row[j-1][0];
                dp[i][j] += row[j-1][1];
                swap(row[j-1][0],row[j-1][1]);
                dp[i][j] -= row[(j-1+k)%(2*k)][0];
                dp[i][j] += row[(j-1+k)%(2*k)][1];
                swap(row[(j-1+k)%(2*k)][0],row[(j-1+k)%(2*k)][1]);
            }
        }
    }

    int ans = 0;
    rep(i,2*k){
        rep(j,2*k){
            //cout << i << ' ' << j << ' ' << dp[i][j] << endl;
            ans = max(ans,dp[i][j]);
        }
    }

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


Measured memory_bytes: 47104