import Control.Applicative
import Control.Monad
import Data.Array.IO
import Data.Array
import qualified Data.ByteString.Char8 as BS
import Data.Maybe

readIntBS = fst . fromJust . BS.readInt
modifyArray arr i f = readArray arr i >>= \x -> writeArray arr i (f x)

main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]

  field <- newArray ((0,0),(2*k,2*k)) 0 :: IO (IOUArray (Int,Int) Int)

  replicateM_ n $ do
    (x,y,c) <- (\[x,y,c] -> (read x, read y, head c)) . words <$> getLine :: IO (Int,Int,Char)
    modifyArray field ((if c=='B' then x else x+k)`mod`(2*k)+1, y`mod`(2*k)+1) (+1)

  forM_ [1..2*k] $ \j -> do
    forM_ [2..2*k] $ \i -> do
      readArray field (i-1,j) >>= \x -> modifyArray field (i,j) (x+)

  forM_ [1..2*k] $ \i -> do
    forM_ [2..2*k] $ \j -> do
      readArray field (i,j-1) >>= \x -> modifyArray field (i,j) (x+)

  --forM_ [0..2*k] $ \j -> do
  --  print =<< unwords . map show <$>  (forM [0..2*k] $ \i -> readArray field (i,j))

  f <- freeze field

  print $ maximum . map (\x -> max x (n-x)) $
    [func f 1 1 (x-1) (y-1) + func f x y (x+k-1) (y+k-1) + func f (x+k) (y+k) (2*k) (2*k) + func f 1 (y+k) (x-1) (2*k) + func f (x+k) 1 (2*k) (y-1) | x<-[1..k], y<-[1..k]]

  where
    func arr x0 y0 x1 y1 = arr!(x1,y1) - arr!(x1,y0-1) - arr!(x0-1,y1) + arr!(x0-1,y0-1)
