#include<bits/stdc++.h>
using namespace std;
 
/// ----------------------------- (Debug) ------------------------------
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i));}
sim, class b dor(pair < b, c > d) {ris << "(" << d.first << ", " << d.second << ")";}
sim dor(rge<c> d) {*this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]";}
#else
sim dor(const c&) { ris; }
#endif
};
vector<char*> tokenizer(const char* args) {
  char *token = new char[111]; strcpy(token, args); token = strtok(token, ", ");
  vector<char*> v({token});
  while(token = strtok(NULL,", ")) v.push_back(token);
  return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char*> args) { cerr << "\b\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char*> args, Head H, Tail... T) {
  debug() << " [" << args.back() << ": " << H << "] ";
  args.pop_back(); debugg(args, T...);
}
#define harg(...) #__VA_ARGS__
#ifndef ONLINE_JUDGE
#define dbg(...) { debugg(tokenizer(harg(__VA_ARGS__, \b\t-->Line)), __VA_ARGS__, __LINE__); cerr << endl;}
#else
#define dbg(...) { }
#endif
/// -----------------------------------------------------------------------

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi> vpi; 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)    
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define REP(i,a,b) for(i=a;i<=b;i++)
#define RAP(i,a,b) for(i=a;i>=b;i--)
#define spa <<" "<<
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
ll mod=1e9+7;
const int MX=0x3f3f3f3f;

int dp[2000][2000][2],k,a[2000][2000][2];

int sum(int x,int y,int i,int j,int c){
	if(x>=i||y>=j) return 0;
	int ans=dp[i][j][c];
	if(x>=0) {
		ans-=dp[x][j][c];
	}
	if(y>=0) {
		ans-=dp[i][y][c];
	}
	if(x>=0&&y>=0) {
		ans+=dp[x][y][c];
	}
	// dbg(x,y,i,j,c,ans);
	// if(i==1&&j==2){
	// 	dbg(x,y,i,j,c,dp[i][j][c]-dp[x][j][c]-dp[i][y][c]+dp[x][y][c]);
	// }
	return ans;
}

int main() {
    IOS;
    int n,i,j,x,y;
    char ch;
    cin>>n>>k;
    REP(i,1,n){
    	cin>>x>>y>>ch;
    	x%=(2*k);
    	y%=(2*k);
    	if(ch=='W') a[x][y][0]++; 
    	else a[x][y][1]++;
    }
    REP(i,0,2*k-1){
    	REP(j,0,2*k-1) {
    		dp[i][j][0]=a[i][j][0];
    		dp[i][j][1]=a[i][j][1];
    		if(i>0) {
    			dp[i][j][0]+=dp[i-1][j][0];
    			dp[i][j][1]+=dp[i-1][j][1];
    		}
    		if(j>0) {
    			dp[i][j][0]+=dp[i][j-1][0];
    			dp[i][j][1]+=dp[i][j-1][1];
    		}
    		if(i>0&&j>0){
    			dp[i][j][0]-=dp[i-1][j-1][0];
    			dp[i][j][1]-=dp[i-1][j-1][1];
    		}
    	}
    }
    // REP(i,0,2*k-1){
    // 	REP(j,0,2*k-1) cerr<<a[i][j][0]<<" ";
    // 	cerr<<"\n";
    // }
    int ans=0;
    REP(i,0,k-1){
    	REP(j,0,k-1){
    		// dbg(i,j,k);
    		if(i==1&&j==2){
    			// dbg(sum(0,0,i,j,1)+sum(0,j,i,j+k,0)+sum(i,0,i+k,j,0)+sum(i,j,i+k,j+k,1)+sum(0,j+k,i,2*k-1,1)+sum(i,j+k,i+k,2*k-1,0)+sum(i+k,0,2*k-1,j,1)+sum(i+k,j,2*k-1,j+k,0)+sum(i+k,j+k,2*k-1,2*k-1,1))
    		}
    		ans=max(ans,sum(-1,-1,i,j,1)+sum(-1,j,i,j+k,0)+sum(i,-1,i+k,j,0)+sum(i,j,i+k,j+k,1)+sum(-1,j+k,i,2*k-1,1)+sum(i,j+k,i+k,2*k-1,0)+sum(i+k,-1,2*k-1,j,1)+sum(i+k,j,2*k-1,j+k,0)+sum(i+k,j+k,2*k-1,2*k-1,1));
    		ans=max(ans,sum(-1,-1,i,j,0)+sum(-1,j,i,j+k,1)+sum(i,-1,i+k,j,1)+sum(i,j,i+k,j+k,0)+sum(-1,j+k,i,2*k-1,0)+sum(i,j+k,i+k,2*k-1,1)+sum(i+k,-1,2*k-1,j,0)+sum(i+k,j,2*k-1,j+k,1)+sum(i+k,j+k,2*k-1,2*k-1,0));
    		// dbg(i,j,ans);
    	}
    }
    cout<<ans;
    return 0;   
} 

Measured memory_bytes: 65964