include[HTML_REMOVED]
using namespace std;
const int N=5000+10;
int f[N][N];
int main(){
int n,r;
cin>>n>>r;
while(n–){
int x,y,w;
cin>>x>>y>>w;
f[x+1][y+1]+=w;
}
for(int i;i[HTML_REMOVED]=N)res=f[N-1][N-1];
for(int i=r;i<N;i)
for(int j=r;j<N;j)
res=max(res,f[i][j]-f[i-r][j]-f[i][j-r]+f[i-r][j-r]);
cout<<res;
return 0;
}