#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

struct CumSum2D(Vec<Vec<i64>>);
impl CumSum2D {
    fn new(data: &Vec<Vec<i64>>) -> CumSum2D {
        let h = data.len();
        let w = data[0].len();
        let mut cs = vec![vec![0; w + 1]; h + 1];
        for i in 0..h {
            for j in 0..w {
                cs[i + 1][j + 1] = data[i][j] + cs[i][j + 1] + cs[i + 1][j] - cs[i][j];
            }
        }
        CumSum2D(cs)
    }
    fn sum_up(&self, x: usize, y: usize) -> i64 {
        let x = std::cmp::min(x, self.0.len());
        let y = std::cmp::min(y, self.0[0].len());
        self.0[x][y]
    }
    fn sum(&self, xrange: std::ops::Range<usize>, yrange: std::ops::Range<usize>) -> i64 {
        if xrange.end <= xrange.start || yrange.end <= yrange.start {
            0
        } else {
            self.sum_up(xrange.end, yrange.end)
                - self.sum_up(xrange.start, yrange.end)
                - self.sum_up(xrange.end, yrange.start)
                + self.sum_up(xrange.start, yrange.start)
        }
    }
}

fn main() {
    let mut sc = Scanner::new();
    let q: usize = sc.cin();
    let k: usize = sc.cin();
    let mut b = vec![vec![0; 2 * k]; 2 * k];
    let mut w = vec![vec![0; 2 * k]; 2 * k];
    for _ in 0..q {
        let x: usize = sc.cin();
        let y: usize = sc.cin();
        let col: String = sc.cin();
        if col == "B" {
            b[x % (2 * k)][y % (2 * k)] += 1;
        } else {
            w[x % (2 * k)][y % (2 * k)] += 1;
        }
    }
    let cs_b = CumSum2D::new(&b);
    let cs_w = CumSum2D::new(&w);

    let mut ans = 0;
    for s in 0..k + 1 {
        for t in 0..k + 1 {
            let a =
                  cs_b.sum(0..t, 0..s)
                + cs_w.sum(0..t, s..s+k)
                + cs_b.sum(0..t, s+k..k+k)
                + cs_w.sum(t..t+k, 0..s)
                + cs_b.sum(t..t+k, s..s+k)
                + cs_w.sum(t..t+k, s+k..k+k)
                + cs_b.sum(t+k..k+k, 0..s)
                + cs_w.sum(t+k..k+k, s..s+k)
                + cs_b.sum(t+k..k+k, s+k..k+k);
            ans = max!(ans, a);
            let a =
                  cs_w.sum(0..t, 0..s)
                + cs_b.sum(0..t, s..s+k)
                + cs_w.sum(0..t, s+k..k+k)
                + cs_b.sum(t..t+k, 0..s)
                + cs_w.sum(t..t+k, s..s+k)
                + cs_b.sum(t..t+k, s+k..k+k)
                + cs_w.sum(t+k..k+k, 0..s)
                + cs_b.sum(t+k..k+k, s..s+k)
                + cs_w.sum(t+k..k+k, s+k..k+k);
            ans = max!(ans, a);
        }
    }
    put!(ans);
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> { self.cin::<String>().chars().collect() }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.cin()).collect() }
}


Measured memory_bytes: 168188