#include <bits/stdc++.h>
using namespace std;
#define de(x) cout<<x<<" ";
#define Pu puts("");
#define sf(x) scanf("%d",&x);
typedef long long ll;
const int N=5e3+10,mod=100003;
const int inf=0x3f3f3f3f;
int pre[N][N];
int n,m,ans;
int main(){
cin>>n>>m;
int x,y,z;
int l=0,r=0;
for(int i=1;i<=n;i++){
sf(x)sf(y)sf(z)
x+=1;y+=1;
if(x>l) l=x;
if(y>r) r=y;
pre[x][y]+=z;
}
l=max(l,m);r=max(r,m);
for(int i=1;i<=l;i++){
for(int j=1;j<=r;j++){
pre[i][j]+=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1];
}
}
int tmp;
for(int i=m;i<=l;i++){
for(int j=m;j<=r;j++){
tmp=pre[i][j]-pre[i-m][j]-pre[i][j-m]+pre[i-m][j-m];
ans=max(ans,tmp);
}
}
printf("%d\n",ans);
return 0;
}