#include <iostream>
#include <algorithm>
using namespace std;const int N = 5010;int arr[N][N] = {0};
int n,r;
int main(){Sios::sync_with_stdio(0),cin.tie(0);cin >> n >> r;r = min(r,5001);int X = r,Y = r; // X Y轴的取值范围 for(int i = 0;i < n;i++){int x,y,w; cin >> x >> y >> w;x++,y++;arr[x][y] += w; // 计算每个位置覆盖的最多价值X = max(X,x), Y = max(Y,y); // 确定 xy轴的取值范围}// 处理前缀和for(int i = 1;i <= X;i++)for(int j = 1;j <= Y;j++)arr[i][j] = arr[i][j] + arr[i-1][j] + arr[i][j-1] - arr[i-1][j-1];// 寻找答案int ans = 0;for(int i = r;i <= X;i++)for(int j = r;j <= Y;j++)ans = max(ans,arr[i][j] - arr[i-r][j] - arr[i][j-r] + arr[i-r][j-r]);cout << ans << endl;return 0;
}