#include<bits/stdc++.h>
using namespace std;
const int N = 5010;
int g[N][N];
int n,r;
int main(){
cin>>n>>r;
r=min(5001,r);
while(n--){
int x,y,w;
cin>>x>>y>>w;
x++,y++;
g[x][y]+=w;
}
for(int i=1;i<=5001;i++)
for(int j=1;j<=5001;j++)
g[i][j]+=g[i-1][j]+g[i][j-1]-g[i-1][j-1];
int res=0;
for(int i=r;i<=5001;i++)
for(int j=r;j<=5001;j++)
res=max(res,g[i][j]-g[i-1][j]-g[i][j-1]+g[i-1][j-1]);
cout<<res<<endl;
return 0;
}