package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

type Request struct {
	x, y  int
	color string
}

type White struct {
	x, y int
}

var wh [4001][4001]int
var sum [4001][4001]int

func main() {
	sc.Split(bufio.ScanWords)
	log.SetFlags(log.Lshortfile)
	N := nextInt()
	K := nextInt()
	for i := 0; i < N; i++ {
		x := nextInt()
		y := nextInt()
		c := nextString()
		if c == "W" {
			y += K
			log.Println(x, y, c)
		}
		wh[y%(2*K)][x%(2*K)]++
	}
	for i := 0; i <= 4*K; i++ {
		for j := 0; j <= 4*K; j++ {
			wh[i][j] = wh[i%(2*K)][j%(2*K)]
		}
	}
	for i := 0; i <= 4*K; i++ {
		for j := 0; j <= 4*K; j++ {
			sum[i][j] = wh[i][j]
			if i-1 >= 0 {
				sum[i][j] += sum[i-1][j]
			}
			if j-1 >= 0 {
				sum[i][j] += sum[i][j-1]
			}
			if i-1 >= 0 && j-1 >= 0 {
				sum[i][j] -= sum[i-1][j-1]
			}
		}
	}
	ans := 0
	for i := 0; i <= 2*K; i++ {
		for j := 0; j <= 2*K; j++ {
			tmp := 0
			tmp += get(i, j, i+K-1, j+K-1)
			tmp += get(i+K, j+K, i+2*K-1, j+2*K-1)
			ans = max(ans, tmp)
		}
	}
	fmt.Println(ans)
}

func get(x1, y1, x2, y2 int) int {
	res := sum[y2][x2]
	if x1 > 0 {
		res -= sum[y2][x1-1]
	}
	if y1 > 0 {
		res -= sum[y1-1][x2]
	}
	if x1 > 0 && y1 > 0 {
		res += sum[y1-1][x1-1]
	}
	return res
}
func nextString() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
