#include <bits/stdc++.h>
using namespace std;
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int qread() {
  register char c =
      (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
           ? EOF
           : *p1++);
  register int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - 48;
    c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  return x * f;
}
inline int Abs(const int& x) { return (x > 0 ? x : -x); }
inline int Max(const int& x, const int& y) { return (x > y ? x : y); }
inline int Min(const int& x, const int& y) { return (x < y ? x : y); }
struct Bot {
  int x, r, q;
  bool operator<(const Bot& b) const { return q < b.q; }
};
struct Opt {
  int type, x, yl, yr;
  Opt() {}
  Opt(int type, int x, int yl, int yr) : type(type), x(x), yl(yl), yr(yr) {}
};
Opt opt[500005], tmp[500005];
int n, k, q, vtop, c[1500005];
long long ans = 0, val[1500005];
Bot a[100005];
int top = 0;
inline void Read() {
  n = qread();
  k = qread();
  for (int i = 1; i <= n; i++) {
    a[i].x = qread();
    a[i].r = qread();
    a[i].q = qread();
  }
}
inline void Prefix() {
  int l = 1, r = 1;
  for (int i = 1; i <= n; i++) {
    while (r <= n && a[r].q <= a[i].q + k) {
      opt[++q] = Opt(1, a[r].x - a[r].r, a[r].x, a[r].x);
      opt[++q] = Opt(-1, a[r].x + a[r].r + 1, a[r].x, a[r].x);
      r++;
    }
    while (l <= n && a[l].q < a[i].q - k) {
      opt[++q] = Opt(-1, a[l].x - a[l].r, a[l].x, a[l].x);
      opt[++q] = Opt(1, a[l].x + a[l].r + 1, a[l].x, a[l].x);
      l++;
    }
    opt[++q] = Opt(0, a[i].x, a[i].x - a[i].r, a[i].x + a[i].r);
  }
  for (int i = 1; i <= q; i++) {
    val[i] = opt[i].yl;
    val[i + q] = opt[i].yr;
    val[i + 2 * q] = opt[i].x;
  }
  sort(val + 1, val + 3 * q + 1);
  vtop = unique(val + 1, val + 3 * q + 1) - val - 1;
  for (int i = 1; i <= q; i++) {
    opt[i].yl = lower_bound(val + 1, val + vtop + 1, opt[i].yl) - val;
    opt[i].yr = lower_bound(val + 1, val + vtop + 1, opt[i].yr) - val;
    opt[i].x = lower_bound(val + 1, val + vtop + 1, opt[i].x) - val;
  }
}
inline int Lowbit(int x) { return x & -x; }
inline void Update(int i, int x) {
  for (int j = i; j <= vtop; j += Lowbit(j)) c[j] += x;
}
inline int Query(int i) {
  int ans = 0;
  for (int j = i; j >= 1; j -= Lowbit(j)) ans += c[j];
  return ans;
}
inline void Clear(int i) {
  for (int j = i; j <= vtop; j += Lowbit(j)) {
    if (!c[j]) break;
    c[j] = 0;
  }
}
inline void Cdq(int l, int r) {
  if (l == r) return;
  int mid = l + r >> 1;
  Cdq(l, mid);
  Cdq(mid + 1, r);
  int i = l, j = mid + 1, k = l;
  while (i <= mid && j <= r) {
    if (opt[i].x <= opt[j].x) {
      if (opt[i].type) Update(opt[i].yl, opt[i].type);
      tmp[k++] = opt[i++];
    } else {
      if (!opt[j].type) ans += Query(opt[j].yr) - Query(opt[j].yl - 1);
      tmp[k++] = opt[j++];
    }
  }
  while (i <= mid) {
    if (opt[i].type) Update(opt[i].yl, opt[i].type);
    tmp[k++] = opt[i++];
  }
  while (j <= r) {
    if (!opt[j].type) ans += Query(opt[j].yr) - Query(opt[j].yl - 1);
    tmp[k++] = opt[j++];
  }
  for (int i = l; i <= mid; i++) Clear(opt[i].yl);
  for (int i = l; i <= r; i++) opt[i] = tmp[i];
}
int main() {
  Read();
  sort(a + 1, a + n + 1);
  Prefix();
  Cdq(1, q);
  printf("%lld", (ans - n) / 2);
  return 0;
}
