import std.algorithm, std.conv, std.range, std.stdio, std.string;

void readV(T...)(ref T t){auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(typeof(v));r.popFront;}}
void readA(T)(size_t n,ref T t){t=new T(n);auto r=readln.splitter;foreach(ref v;t){v=r.front.to!(ElementType!T);r.popFront;}}
void readM(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=readln.splitter;foreach(ref v;t){v[i]=r.front.to!(ElementType!(typeof(v)));r.popFront;}}}
void readS(T)(size_t n,ref T t){t=new T(n);foreach(ref v;t){auto r=readln.splitter;foreach(ref j;v.tupleof){j=r.front.to!(typeof(j));r.popFront;}}}

void main()
{
  int n, k; readV(n, k);

  auto k2 = k*2, cb = new int[][](k2, k2), cw = new int[][](k2, k2);
  foreach (_; 0..n) {
    int x, y; string s; readV(x, y, s);
    x %= k2; y %= k2;
    if (s == "B") cb[y][x]++;
    else          cw[y][x]++;
  }

  auto cbs = new int[][](k2+1, k2+1), cws = new int[][](k2+1, k2+1);
  foreach (i; 0..k2)
    foreach (j; 0..k2) {
      cbs[i+1][j+1] = cbs[i][j+1]+cbs[i+1][j]-cbs[i][j]+cb[i][j];
      cws[i+1][j+1] = cws[i][j+1]+cws[i+1][j]-cws[i][j]+cw[i][j];
    }

  auto r = 0;
  foreach (i; 0..k+1)
    foreach (j; 0..k+1) {
      auto tw = cws[k2][k2]-cws[i+k][k2]-cws[k2][j+k]+cws[i+k][j+k]*2-cws[i][j+k]-cws[i+k][j]+cws[i][j]*2;
      auto tb = cbs[k2][k2]-cbs[i+k][k2]-cbs[k2][j+k]+cbs[i+k][j+k]*2-cbs[i][j+k]-cbs[i+k][j]+cbs[i][j]*2;
      r = max(r, tw+(cbs[k2][k2]-tb));
    }

  writeln(r);
}
