#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }

fn main() {
    let (n, k) = get!(usize, i64);
    let xyc = get!(i64, i64, String; n);
    let xy: Vec<(i64, i64)> = xyc.into_iter()
        .map(|(x, y, c)| if c == "W" { (x, y + k) } else { (x, y) })
        .map(|(x, y)| (x % (2 * k), y % (2 * k)))
        .collect();

    let mut imos = vec![vec![0i64; 2 * k as usize + 1]; 2 * k as usize + 1];

    for &(x, y) in &xy {
        let l = max(x - k + 1, 0);
        let r = x;
        let b = max(y - k + 1, 0);
        let u = y;

        imos[b as usize][l as usize] += 1;
        imos[u as usize + 1][r as usize + 1] += 1;
        imos[b as usize][r as usize + 1] -= 1;
        imos[u as usize + 1][l as usize] -= 1;
    }

    // ru
    for &(x, y) in &xy {
        if x < k || y < k {
            continue;
        }

        let l = 0;
        let r = x - k;
        let b = 0;
        let u = y - k;
        imos[b as usize][l as usize] += 1;
        imos[u as usize + 1][r as usize + 1] += 1;
        imos[b as usize][r as usize + 1] -= 1;
        imos[u as usize + 1][l as usize] -= 1;
    }

    // lu
    for &(x, y) in &xy {
        if y < k || x == 2 * k - 1 {
            continue;
        }

        let l = x + 1;
        let r = min(l + k - 1, 2 * k - 1);
        let b = 0;
        let u = y - k;
        imos[b as usize][l as usize] += 1;
        imos[u as usize + 1][r as usize + 1] += 1;
        imos[b as usize][r as usize + 1] -= 1;
        imos[u as usize + 1][l as usize] -= 1;
    }

    // rb
    for &(x, y) in &xy {
        if x < k || y == 2 * k - 1 {
            continue;
        }

        let l = 0;
        let r = x - k;
        let b = y + 1;
        let u = min(b + k - 1, 2 * k - 1);
        imos[b as usize][l as usize] += 1;
        imos[u as usize + 1][r as usize + 1] += 1;
        imos[b as usize][r as usize + 1] -= 1;
        imos[u as usize + 1][l as usize] -= 1;
    }

    // lb
    for &(x, y) in &xy {
        if x == 2 * k - 1 || y == 2 * k - 1 {
            continue;
        }

        let l = x + 1;
        let r = min(l + k - 1, 2 * k - 1);
        let b = y + 1;
        let u = min(b + k - 1, 2 * k - 1);
        imos[b as usize][l as usize] += 1;
        imos[u as usize + 1][r as usize + 1] += 1;
        imos[b as usize][r as usize + 1] -= 1;
        imos[u as usize + 1][l as usize] -= 1;
    }
    // b
    for &(x, y) in &xy {
        if y >= k - 1 {
            continue;
        }

        let l = max(x - k + 1, 0);
        let r = x;
        let b = k + y + 1;
        let u = 2 * k - 1;
        imos[b as usize][l as usize] += 1;
        imos[u as usize + 1][r as usize + 1] += 1;
        imos[b as usize][r as usize + 1] -= 1;
        imos[u as usize + 1][l as usize] -= 1;
    }

    // l
    for &(x, y) in &xy {
        if x >= k - 1 {
            continue;
        }

        let l = k + x + 1;
        let r = 2 * k - 1;
        let b = max(y - k + 1, 0);
        let u = y;
        imos[b as usize][l as usize] += 1;
        imos[u as usize + 1][r as usize + 1] += 1;
        imos[b as usize][r as usize + 1] -= 1;
        imos[u as usize + 1][l as usize] -= 1;
    }

    for i in 0..2 * k as usize + 1 {
        for j in 1..2 * k as usize + 1 {
            imos[i][j] += imos[i][j - 1];
        }
    }

    for i in 0..2 * k as usize + 1 {
        for j in 1..2 * k as usize + 1 {
            imos[j][i] += imos[j - 1][i];
        }
    }

    let ans = imos.into_iter()
        .map(|v| v.iter().max().cloned().unwrap())
        .max()
        .unwrap();
    println!("{}", ans);
}


Measured memory_bytes: 45308