#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <map>
#include <set>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 5e7+5;
const int Max = 0x3f3f3f;
const ll MOD = 1e9+7;
const int N = 5010;
vector<string> s;
int mapp[N][N];
int main() {
IOS;
int n, r, x, y, v;
int maxnn = 0;
cin >> n >> r;
int xx = r, yy = r;//先确定最小边界为给定的正方形的边长
for (int i = 0; i < n; i++) {
cin >> x >> y >> v;
x++; y++;
mapp[x][y] = v;
//更新最小边界
xx = max(xx, x);
yy = max(yy, y);
}
for (int i = 1; i <= xx; i++) {
for (int j = 1; j <= yy; j++) {
mapp[i][j]=mapp[i-1][j]+mapp[i][j-1]-mapp[i-1][j-1]+mapp[i][j];
}
}
for (int i = r; i <= xx; i++){
for (int j = r; j <= yy; j++){
maxnn = max(maxnn, mapp[i][j]-mapp[i-r][j]-mapp[i][j-r]+mapp[i-r][j-r]);
}
}
cout << maxnn <<endl;
return 0;
}