let () =
  Scanf.scanf "%d %d" @@ fun n k ->
  let kk = 2*k in
  let a = Array.init n (fun _ ->
    Scanf.scanf " %d %d %c" (fun x y z -> (x+1) mod kk, (y+1) mod kk, z)) in

  let b = Array.init (kk+1) (fun _ -> Array.make (kk+1) 0) in
  let fill lx ly ux uy = begin
    let r x = x |> max 0 |> min kk in
    let lx, ly = r lx, r ly in
    let ux, uy = r ux, r uy in
    b.(lx).(ly) <- b.(lx).(ly) + 1; b.(ux).(ly) <- b.(ux).(ly) - 1;
    b.(lx).(uy) <- b.(lx).(uy) - 1; b.(ux).(uy) <- b.(ux).(uy) + 1;
  end in

  a |> Array.iter (fun (x,y,c) ->
    let x, y = match c with
      | 'B' -> x, y
      | _   -> (x+k) mod kk, y in
    [(-k,-k); (-k,k); (0,-kk); (0,0); (k,-k); (k,k)] |> List.iter @@ fun (dx,dy) ->
      fill (x+dx) (y+dy) (x+dx+k) (y+dy+k)
  );

  for i = 0 to kk do
    for j = 0 to kk do
      if i > 0 then b.(i).(j) <- b.(i).(j) + b.(i-1).(j);
      if j > 0 then b.(i).(j) <- b.(i).(j) + b.(i).(j-1);
      if i > 0 && j > 0 then b.(i).(j) <- b.(i).(j) - b.(i-1).(j-1);
  done done;
  Array.fold_left (Array.fold_left max) 0 b |> Printf.printf "%d\n"