#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "inline", "no-stack-protector")
using namespace std;
int rd() {
  int x;
  char c(getchar());
  bool k;
  while (!isdigit(c) && c ^ '-')
    if ((c = getchar()) == EOF) exit(0);
  c ^ '-' ? (k = 1, x = c & 15) : k = x = 0;
  while (isdigit((c = getchar()))) x = x * 10 + (c & 15);
  return k ? x : -x;
}
void wr(long long a) {
  if (a < 0) putchar('-'), a = -a;
  if (a <= 9)
    putchar(a | '0');
  else
    wr(a / 10), putchar((a % 10) | '0');
}
signed const INF(0x3f3f3f3f), NINF(0xc3c3c3c3);
long long const LINF(0x3f3f3f3f3f3f3f3fLL), LNINF(0xc3c3c3c3c3c3c3c3LL);
int n, k, d[(100010)], t[(100010)];
long long ans;
struct R {
  int x, r, q, i, p, lp, rp;
} a[(100010)];
void mdf(int x, int v) {
  while (x <= n) t[x] += v, x += x & -x;
}
int qry(int x) {
  int r(0);
  while (x) r += t[x], x ^= x & -x;
  return r;
}
void cdq(int l, int r);
signed main() {
  (n = rd()), (k = rd());
  for (int i(1); i <= (n); ++i) a[i] = {d[i] = rd(), rd(), rd(), i};
  sort(d + 1, d + n + 1),
      sort(a + 1, a + n + 1, [](R a, R b) { return a.r > b.r; });
  for (int i(1); i <= (n); ++i) {
    a[i].p = lower_bound(d + 1, d + n + 1, a[i].x) - d;
    a[i].lp = lower_bound(d + 1, d + n + 1, a[i].x - a[i].r) - d;
    a[i].rp = upper_bound(d + 1, d + n + 1, a[i].x + a[i].r) - d - 1;
  }
  cdq(1, n), wr(ans), exit(0);
}
void cdq(int l, int r) {
  if (l == r) return;
  int m((l + r) >> 1), p(l), q(l);
  cdq(l, m), cdq(m + 1, r);
  for (int i(m + 1); i <= (r); ++i) {
    while (q <= m && a[q].q - a[i].q <= k) mdf(a[q++].p, 1);
    while (p < q && a[i].q - a[p].q > k) mdf(a[p++].p, -1);
    ans += qry(a[i].rp) - qry(a[i].lp - 1);
  }
  for (int i(p); i < (q); ++i) mdf(a[i].p, -1);
  sort(a + l, a + r + 1, [](R a, R b) { return a.q < b.q; });
}
