#include <bits/stdc++.h>
int n, k;
std::pair<int, std::pair<int, int> > s[300100];
std::set<std::pair<int, int> > left, right;
std::pair<int, std::pair<int, int> > timeline[300100];
int x_value[300100];
std::map<int, int> mapx;
std::set<int> sx;
int bit[300100];
int read(int pos) {
  int ret = 0;
  while (pos > 0) {
    ret += bit[pos];
    pos -= pos & -pos;
  }
  return ret;
}
void update(int pos, int val) {
  while (pos < 300100) {
    bit[pos] += val;
    pos += pos & -pos;
  }
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &s[i].second.first, &s[i].second.second, &s[i].first);
    x_value[i] = s[i].second.first;
    sx.insert(x_value[i]);
  }
  std::sort(x_value, x_value + n);
  for (int i = 0; i < n; i++) mapx[x_value[i]] = i + 1;
  sort(s, s + n);
  s[n].first = 10100;
  int ul, ll, r;
  ul = ll = r = 0;
  long long ret = 0;
  while (r < n) {
    int freq = s[r].first;
    right.clear();
    while (s[r].first == freq) right.insert(s[r++].second);
    while (freq - s[ll].first > k) left.erase(s[ll++].second);
    while (s[ul].first - freq <= k) left.insert(s[ul++].second);
    int tsize = 0;
    std::set<std::pair<int, int> >::iterator it;
    for (it = left.begin(); it != left.end(); it++) {
      int x = it->first, r = it->second;
      timeline[tsize++] =
          std::pair<int, std::pair<int, int> >(x, std::pair<int, int>(1, x));
      timeline[tsize++] = std::pair<int, std::pair<int, int> >(
          x + r, std::pair<int, int>(3, x));
    }
    for (it = right.begin(); it != right.end(); it++) {
      int x = it->first, r = it->second;
      timeline[tsize++] =
          std::pair<int, std::pair<int, int> >(x, std::pair<int, int>(2, r));
    }
    sort(timeline, timeline + tsize);
    for (int t = 0; t < tsize; t++) {
      int type = timeline[t].second.first;
      int payload = timeline[t].second.second;
      if (type == 1) update(mapx[payload], 1);
      if (type == 3) update(mapx[payload], -1);
      if (type == 2) {
        int x = timeline[t].first;
        int r = payload;
        int id = mapx[*sx.lower_bound(x)] - 1;
        int q1 = read(id);
        id = mapx[*sx.lower_bound(x - r)] - 1;
        int q2 = read(id);
        ret += q1 - q2;
      }
    }
  }
  std::cout << ret << std::endl;
  return 0;
}
