let () =
  let n, k = Scanf.scanf "%d %d\n" (fun n k -> n, k) in
  let xycs = Array.init n (fun _ -> Scanf.scanf "%d %d %c\n" (fun x y c -> x mod (2 * k), y mod (2 * k), c)) in
  let candidates = Array.make_matrix (2 * k) (2 * k) 0 in
  Array.iter (function (x, y, c) ->
    begin if c = 'W' then [ ((0, k), (0, k)); ((k, 2 * k), (k, 2 * k)) ]
    else [ ((k, 2 * k), (0, k)); ((0, k), (k, 2 * k)) ] end
    |> List.map (fun (((x, x'), (y, y')) as self) -> [ ((x + 2 * k, x' + 2 * k), (y + 2 * k, y' + 2 * k)); self])
    |> List.concat
    |> List.map (fun ((i, i'), (j, j')) -> ((i - x, i' - x), (j - y, j' - y)))
    |> List.filter (fun ((x, x'), (y, y')) -> 0 < x' && x < 2 * k && 0 < y' && y < 2 * k)
    |> List.iter (fun ((x, x'), (y, y')) ->
        candidates.(max 0 x).(max 0 y) <- candidates.(max 0 x).(max 0 y) + 1;
        begin if x' < 2 * k then
          candidates.(x').(max 0 y) <- candidates.(x').(max 0 y) - 1 end;
        begin if y' < 2 * k then
          candidates.(max 0 x).(y') <- candidates.(max 0 x).(y') - 1 end;
        begin if x' < 2 * k && y' < 2 * k then
          candidates.(x').(y') <- candidates.(x').(y') + 1 end)) xycs;
  for j = 0 to 2 * k - 1 do
    for i = 1 to 2 * k - 1 do
      candidates.(i).(j) <- candidates.(i - 1).(j) + candidates.(i).(j)
    done
  done;
  for i = 0 to 2 * k - 1 do
    for j = 1 to 2 * k - 1 do
      candidates.(i).(j) <- candidates.(i).(j - 1) + candidates.(i).(j)
    done
  done;
  Array.map (Array.fold_left max 0) candidates
  |> Array.fold_left max 0
  |> Printf.printf "%d\n"
