
#include <cstdlib>
#include <fstream>
#include <stdlib.h>

using namespace std;

#define MAX 50005

// x i y su koordinate, a z je broj stanovnika
typedef struct{
        int x, y, z;
} city;


// koristi se za uporedjivanje gradova po x koordinati
// zbog quick sorta
int up(const void *px, const void* py){
    city* a = (city*) px;
    city* b = (city*) py;
    if (a->x < b->x) return -1;
    if (a->x > b->x) return 1;
    return 0;
}

int main(int argc, char *argv[])
{
    
    ifstream in("autoput.in");
    ofstream out("autoput.out");
    int x, y, z, n, d, i;
    int max = 0, sum = 0, l, r;
    city gradovi[MAX];
    
    in >> n >> d;
    for (i = 0; i < n; i++){
        city c;
        in >> c.x >> c.y >> c.z;
        gradovi[i] = c;
    }
    qsort(gradovi, n, sizeof(city), up);
    
    l = 0; max = gradovi[0].z; sum = gradovi[0].z;
    for (r = 1; r < n; r++){
        while (gradovi[r].x-gradovi[l].x>2*d)
              sum -= gradovi[l++].z;
        sum += gradovi[r].z;
        max >?= sum;
    }
    out << max << endl;
    in.close();
    out.close();
    return EXIT_SUCCESS;
}
