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, y, 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, 0); (k, k) ]
    else [ (0, k); (k, 0) ] end
    |> List.map (fun (i, j) -> ((i - x + 2 * k * x) mod (2 * k), (j - y + 2 * k * y) mod (2 * k)))
    |> List.map (fun (x, y) ->
        if x < k then [(x, y)] else [(x, y); (x - 2 * k, y)])
    |> List.concat
    |> List.map (fun (x, y) ->
        if y < k then [(x, y)] else [(x, y); (x, y - 2 * k)])
    |> List.concat
    |> List.iter (fun (x, y) ->
        candidates.(max 0 x).(max 0 y) <- candidates.(max 0 x).(max 0 y) + 1;
        begin if x < k then
          candidates.(x + k).(max 0 y) <- candidates.(x + k).(max 0 y) - 1 end;
        begin if y < k then
          candidates.(max 0 x).(y + k) <- candidates.(max 0 x).(y + k) - 1 end;
        begin if x < k && y < k then
          candidates.(x + k).(y + k) <- candidates.(x + k).(y + k) + 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"