#include <bits/stdc++.h>
const int M = 200005;
const int INF = 1e9;
const double alp = 0.8;
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0, w = 1;
  while (c > '9' || c < '0') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * w;
}
int n, m, K;
int top, st[M], tot, rtp;
long long ans;
struct Poi {
  int x[2];
} p[M];
struct Person {
  int x, r, q;
} pi[M];
struct Node {
  Poi tp;
  int lsn, rsn, sz, tmi[2], tmx[2];
} t[M];
inline bool operator<(Person a, Person b) { return a.r > b.r; }
inline bool operator<(Poi a, Poi b) { return a.x[K] < b.x[K]; }
inline int New() { return ++tot; }
inline void pushup(int now) {
  for (int k = 0; k <= 1; k++) {
    t[now].tmi[k] = t[now].tmx[k] = t[now].tp.x[k];
    if (t[now].lsn) {
      t[now].tmi[k] = min(t[now].tmi[k], t[t[now].lsn].tmi[k]);
      t[now].tmx[k] = max(t[now].tmx[k], t[t[now].lsn].tmx[k]);
    }
    if (t[now].rsn) {
      t[now].tmi[k] = min(t[now].tmi[k], t[t[now].rsn].tmi[k]);
      t[now].tmx[k] = max(t[now].tmx[k], t[t[now].rsn].tmx[k]);
    }
  }
  t[now].sz = t[t[now].lsn].sz + t[t[now].rsn].sz + 1;
}
int build(int l, int r, int W) {
  if (l > r) return 0;
  K = W;
  int cnt = New(), mid = (l + r) >> 1;
  nth_element(p + l, p + mid, p + r + 1);
  t[cnt].tp = p[mid];
  t[cnt].lsn = build(l, mid - 1, (W ^ 1));
  t[cnt].rsn = build(mid + 1, r, (W ^ 1));
  pushup(cnt);
  return cnt;
}
void Ins(Poi po, int &now, int W) {
  if (!now) {
    now = New();
    t[now].tp = po;
    pushup(now);
    return;
  }
  if (po.x[W] <= t[now].tp.x[W])
    Ins(po, t[now].lsn, (W ^ 1));
  else
    Ins(po, t[now].rsn, (W ^ 1));
  pushup(now);
}
inline bool Allin(int ax, int ay, int bx, int by, int Ax, int Ay, int Bx,
                  int By) {
  return (Ax >= ax && Ay >= ay && Bx <= bx && By <= by);
}
inline bool Allout(int ax, int ay, int bx, int by, int Ax, int Ay, int Bx,
                   int By) {
  return (Ax > bx || Ay > by || Bx < ax || By < ay);
}
int query(int lx, int ly, int rx, int ry, int now) {
  if (!now) return 0;
  if (Allin(lx, ly, rx, ry, t[now].tmi[0], t[now].tmi[1], t[now].tmx[0],
            t[now].tmx[1]))
    return t[now].sz;
  if (Allout(lx, ly, rx, ry, t[now].tmi[0], t[now].tmi[1], t[now].tmx[0],
             t[now].tmx[1]))
    return 0;
  int ret = 0;
  if (Allin(lx, ly, rx, ry, t[now].tp.x[0], t[now].tp.x[1], t[now].tp.x[0],
            t[now].tp.x[1]))
    ret++;
  ret += query(lx, ly, rx, ry, t[now].lsn) + query(lx, ly, rx, ry, t[now].rsn);
  return ret;
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++) {
    pi[i].x = read();
    pi[i].r = read();
    pi[i].q = read();
  }
  sort(pi + 1, pi + n + 1);
  Poi po;
  for (int i = 1; i <= n; i++) {
    ans += query(pi[i].x - pi[i].r, pi[i].q - m, pi[i].x + pi[i].r, pi[i].q + m,
                 rtp);
    p[i].x[0] = pi[i].x;
    p[i].x[1] = pi[i].q;
    Ins(p[i], rtp, 0);
    if (i % 5001 == 0) {
      tot = 0;
      rtp = build(1, i, 0);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
