{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe
import Data.List
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Control.Monad
import Control.Monad.ST

solve :: Int -> Int -> [(Int,Int,String)] -> Int
solve n k xycs = answer where
  _cell :: VU.Vector Int
  _cell = VU.create $ do
    vec <- VUM.replicate (k*k) 0
    forM_ (map (absPt k) xycs) $ \(x,y,t) -> VUM.modify vec (+ t) (enc x y)
    return vec
  
  enc :: Int -> Int -> Int
  enc x y = x*k + y
  cell x y = _cell VU.! (enc x y)

  bars0 = [sum [cell i j | i <- [0..k-1]] | j <- [0..k-1]]
  val0 = sum bars0

  starts = unfoldr f (0, val0, bars0) where
    f (i, val, bars) | i == k = Nothing
                     | otherwise = Just ((val, bars), (i+1, val', bars'))
      where
      bars' = map g (zip [0..] bars) where
        g (j,x) = x - 2 * cell i j
      val' = val - sum [2 * cell i j | j <- [0..k-1]]

  calcV (val, bars) = maximum (map abs (scanl' (-) val (map (* 2) bars)))

  answerXX = (n + (maximum (map calcV starts))) `div` 2

  answer = answerXX

absPt k (x,y,c) =
  let (dx, mx) = divMod x k
      (dy, my) = divMod y k
      t | (head c == 'B') `xor` (dx `mod` 2 == 1) `xor` (dy `mod` 2 == 1) =  1
        | otherwise                                                       = -1
  in (mx, my, t)

xor :: Bool -> Bool -> Bool
xor x y = ((not x) && y) || (x && not y)

readBInt :: B.ByteString -> Int
readBInt = fst . fromJust . B.readInt

tmain :: B.ByteString -> Int
tmain cont =
  let remLines0 = map B.words (B.lines cont)
      [bs_n,bs_k]:remLines1 = remLines0
      n = readBInt bs_n
      k = readBInt bs_k
      xycs = map (\[x1,x2,x3] -> (readBInt x1,readBInt x2,B.unpack x3)) remLines1
  in solve n k xycs

outAnswer :: Int -> IO ()
outAnswer = putStrLn . show

main :: IO ()
main = outAnswer . tmain =<< B.getContents
