// see https://github.com/LumaKernel/vimfiles/tree/master/snippets/cp-cpp/temp.snip
const bool DEBUG = 0;
/// --- .lumrc Template {{{ ///
#include <iostream>
#include <vector>
#include <array>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <tuple>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <complex>
#include <random>
#include <iomanip>
#include <cassert>
#include <functional>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = tuple<ll, ll>;
using P3 = tuple<ll, ll, ll>;
using VI = vector<int>;
using VL = vector<ll>;
using VP = vector<P>;
using VS = vector<string>;

#define omajinai ios::sync_with_stdio(false);cin.tie(0)

#define FOR(i,a,b) for(int i=int(a);i<int(b);++i)
#define FORI(i,a,b) for(int i=int(a);i<=int(b);++i)
#define REP(i,n) FOR(i,0,n)
#define REPI(i,n) FORI(i,0,n)
#define RFOR(i,a,b) for(int i=int(b)-1;i>=int(a);--i)
#define RFORI(i,a,b) for(int i=int(b);i>=int(a);--i)
#define RREP(i,n) RFOR(i,0,n)
#define RREPI(i,n) RFORI(i,0,n)

#define ALL(a) (a).begin(),(a).end()
#define UNIQUE(a) (a).erase(unique(ALL(a)),(a).end())
#define PB push_back
#define EACH(i,c) REP(i,(c).size())
#define REACH(i,c) RREP(i,(c).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(ALL(c))

#define BR cout<<"\n";

#define dump(x) if(DEBUG) cerr<<"["<<__LINE__<< "] "<<#x<<"="<<(x)<<"\n";
#define dump2(x,y) if(DEBUG) cerr<<"["<<__LINE__<< "] "<<#x<<"="<<(x)\
  <<" , "<<#y<<"="<<(y)<<"\n";
#define dump3(x,y,z) if(DEBUG)cerr<<"["<<__LINE__<<"] "<<#x<<"="<<(x)\
  <<" , "<<#y<<"="<<(y)\
<<" , "<<#z<<"="<<(z)<<"\n";

#define SAY(x) if(DEBUG) cerr<<"["<<__LINE__<< "] "<<(x)<<"\n";

#define YES(x) cout<<((x)?"YES":"NO")<<"\n";
#define Yes(x) cout<<((x)?"Yes":"No")<<"\n";
#define yes(x) cout<<((x)?"yes":"no")<<"\n";

inline int omajinai_int_in(){omajinai;int n;cin>>n;return n;}
inline ll omajinai_ll_in(){omajinai;ll n;cin>>n;return n;}
inline string omajinai_string_in(){omajinai;string n;cin>>n;return n;}
inline int int_in(){int n;cin>>n;return n;}
inline ll ll_in(){ll n;cin>>n;return n;}
inline string string_in(){string n;cin>>n;return n;}
#define oini omajinai_int_in()
#define oinl omajinai_ll_in()
#define oins omajinai_string_in()
#define ini int_in()
#define inl ll_in()
#define ins string_in()

#define isInside(y,x) (0<=(y)&&(y)<h&&0<=(x)&&(x)<w)

#define fi(x) (get<0>(x))
#define se(x) (get<1>(x))
#define th(x) (get<2>(x))
#define fo(x) (get<3>(x))
#define fif(x) (get<4>(x))

template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; EACH(i, v) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]";  return o; }

template <typename T> inline void smax(T &a, T b) { a = a > b ? a : b; }
template <typename T> inline void smin(T &a, T b) { a = a < b ? a : b; }

/// }}}--- ///

constexpr int INF = 1e9+1;
constexpr ll LINF = 1e18+1;
constexpr int MOD = 1e9+7;


ll ans;

int n=ini,k=ini;
  int k2=k*2;

const int K=1e3;

ll wws[K*2][K*2];
ll bs[K*2][K*2];

ll cnt(ll g[K*2][K*2], int x1, int y1, int x2, int y2) {
  ll res = 0;
  if(x2>=0&&y2>=0)res+=g[x2][y2];
  if(x1-1>=0&&y2>=0)res -= g[x1-1][y2];
  if(y1-1>=0&&x2>=0)res -= g[x2][y1-1];
  if(x1-1>=0&&y1-1>=0)res += g[x1-1][y1-1];
  return res;
}

int main() {
  dump3(n,k,k2);
  REP(i,n) {
    int x=ini,y=ini;
    bool isB=ins=="B";
    (isB?bs:wws)[x%k2][y%k2]++;
  }
  dump(1);
  FOR(x,0,k)FOR(y, k, k2) {
    wws[x][y-k]+=bs[x][y];
    bs[x][y-k]+=wws[x][y];
  }
  dump(1);
  FOR(y,0,k)FOR(x, k, k2) {
    wws[x-k][y]+=bs[x][y];
    bs[x-k][y]+=wws[x][y];
  }
  FOR(y,k,k2)FOR(x, k, k2) {
    wws[x-k][y-k]+=wws[x][y];
    bs[x-k][y-k]+=bs[x][y];
  }
  // imos
  FOR(x,1, k) REP(y, k) {
    wws[x][y] += wws[x-1][y];
    bs[x][y] += bs[x-1][y];
  }
  FOR(y,1, k) REP(x, k) {
    wws[x][y] += wws[x][y-1];
    bs[x][y] += bs[x][y-1];
  }
  // search
  REP(x,k)REP(y,k) {
    ll res;
    res=0;
    res+=cnt(wws,0,0,x-1,y-1);
    res+=cnt(wws,x,y,k-1,k-1);
    res+=cnt(bs,x,0,k-1,y-1);
    res+=cnt(bs,0,y,x-1,k-1);
    smax(ans, res);
    res=0;
    res+=cnt(bs,0,0,x-1,y-1);
    res+=cnt(bs,x,y,k-1,k-1);
    res+=cnt(wws,x,0,k-1,y-1);
    res+=cnt(wws,0,y,x-1,k-1);
    smax(ans, res);
  }
  cout << ans << endl;
}

//


Measured memory_bytes: 62720