import java.util.*;
class Main{
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
int N=5051;
int a[][]=new int [N][N];
int f[][]=new int [N][N];
int n=in.nextInt();
int c=in.nextInt();
while(n–!=0) {
int x=in.nextInt();
int y=in.nextInt();
int w=in.nextInt();
a[x+1][y+1]+=w;
}
for(int i=1;i<N;i) {
for(int j=1;j<f.length;j) {
f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1]+a[i][j];
}
}
int res=0;
for(int i=c;i<f.length;i) {
for(int j=c;j<f.length;j) {
res=Math.max(res, f[i][j]-f[i-c][j]-f[i][j-c]+f[i-c][j-c]);
}
}
System.out.println(res);
}
}