#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
const int MAXF = 10000;
typedef struct R {
  int x, r, f, xid;
} R;
bool fcmp(const R &a, const R &b) {
  if (a.f != b.f) return a.f < b.f;
  return a.x < b.x;
}
bool scmp(const R &a, const R &b) {
  if (a.x + a.r != b.x + b.r) return a.x + a.r > b.x + b.r;
  return a.x < b.x;
}
int n, mxdiff;
R r[MAXN];
R q[MAXN];
int cnt[MAXF + 1];
int off[MAXF + 2];
int pos[MAXF + 1];
int x[MAXN], nx;
int bit[MAXN + 1];
int mark[MAXN + 1], curmark;
void binc(int x) {
  int idx = x + 1;
  while (idx <= nx) {
    if (mark[idx] != curmark) mark[idx] = curmark, bit[idx] = 0;
    ++bit[idx];
    idx += idx & -idx;
  }
}
int bget(int x) {
  int idx = x + 1;
  int ret = 0;
  while (idx > 0) {
    if (mark[idx] != curmark) mark[idx] = curmark, bit[idx] = 0;
    ret += bit[idx];
    idx -= idx & -idx;
  }
  return ret;
}
int bget(int lx, int rx) {
  if (lx > rx) return 0;
  return bget(rx) - bget(lx - 1);
}
long long solve() {
  sort(r, r + n, fcmp);
  nx = 0;
  for (int i = (0); i < (n); ++i) x[nx++] = r[i].x;
  sort(x, x + nx);
  nx = unique(x, x + nx) - x;
  for (int i = (0); i < (n); ++i) r[i].xid = lower_bound(x, x + nx, r[i].x) - x;
  memset(cnt, 0, sizeof(cnt));
  for (int i = (0); i < (n); ++i) ++cnt[r[i].f];
  for (int i = (0); i <= (MAXF); ++i) off[i + 1] = off[i] + cnt[i];
  for (int i = (0); i < (n); ++i) q[i] = r[i];
  for (int i = (0); i <= (MAXF); ++i) sort(q + off[i], q + off[i + 1], scmp);
  long long ret = 0;
  memset(mark, -1, sizeof(mark));
  curmark = -1;
  for (int at = 0, to = at; at < n; at = to) {
    int f = r[at].f;
    ++curmark;
    while (to < n && r[to].f == f) ++to;
    int fmn = max(0, f - mxdiff), fmx = min(MAXF, f + mxdiff);
    for (int j = (fmn); j <= (fmx); ++j) pos[j] = 0;
    for (int i = to - 1; i >= at; --i) {
      for (int j = (fmn); j <= (fmx); ++j)
        while (pos[j] < cnt[j] &&
               q[off[j] + pos[j]].x + q[off[j] + pos[j]].r >= r[i].x)
          binc(q[off[j] + (pos[j]++)].xid);
      int lxid = lower_bound(x, x + nx, r[i].x - r[i].r) - x,
          rxid = r[i].xid - 1;
      int cur = bget(lxid, rxid);
      ret += cur;
    }
  }
  return ret;
}
void run() {
  scanf("%d%d", &n, &mxdiff);
  for (int i = (0); i < (n); ++i) scanf("%d%d%d", &r[i].x, &r[i].r, &r[i].f);
  printf("%lld\n", solve());
}
int main() {
  run();
  return 0;
}
