#include <bits/stdc++.h>
using namespace std;
inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 100000, stdin);
    if (p1 == p2) return EOF;
  }
  return *p1++;
}
inline void read(int &x) {
  char c = nc(), b = 1;
  for (; !(c >= '0' && c <= '9'); c = nc())
    if (c == '-') b = -1;
  for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
    ;
  x *= b;
}
const int N = 1e5 + 5;
struct lala {
  int x, r, f;
  void read() {
    ::read(x);
    ::read(r);
    ::read(f);
  }
  bool operator<(const lala &B) const { return r > B.r; }
};
int n, K;
long long Ans;
lala a[N];
vector<int> T[N << 2];
int tmp[N];
inline bool cmp(int x, int y) { return a[x].x < a[y].x; }
inline void Modify(int x, int l, int r, int t) {
  T[x].push_back(a[tmp[t]].f);
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (t <= mid)
    Modify(x << 1, l, mid, t);
  else
    Modify(x << 1 | 1, mid + 1, r, t);
}
inline void Erase(int x, int l, int r) {
  T[x].clear();
  if (l == r) return;
  int mid = (l + r) >> 1;
  Erase(x << 1, l, mid);
  Erase(x << 1 | 1, mid + 1, r);
}
inline void Sort(int x, int l, int r) {
  sort(T[x].begin(), T[x].end());
  if (l == r) return;
  int mid = (l + r) >> 1;
  Sort(x << 1, l, mid);
  Sort(x << 1 | 1, mid + 1, r);
}
int F, ql, qr;
inline void Query(int x, int l, int r) {
  if (ql <= l && r <= qr) {
    vector<int>::iterator lp = lower_bound(T[x].begin(), T[x].end(), F - K);
    vector<int>::iterator rp = upper_bound(T[x].begin(), T[x].end(), F + K);
    Ans += rp - lp;
    return;
  }
  int mid = (l + r) >> 1;
  if (ql <= mid) Query(x << 1, l, mid);
  if (qr > mid) Query(x << 1 | 1, mid + 1, r);
}
int tx[N];
inline void Solve(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  int m = 0;
  for (int i = l; i <= mid; i++) tmp[++m] = i;
  sort(tmp + 1, tmp + m + 1, cmp);
  for (int i = 1; i <= m; i++) Modify(1, 1, m, i), tx[i] = a[tmp[i]].x;
  Sort(1, 1, m);
  for (int i = mid + 1; i <= r; i++) {
    int lp = lower_bound(tx + 1, tx + m + 1, a[i].x - a[i].r) - tx;
    int rp = upper_bound(tx + 1, tx + m + 1, a[i].x + a[i].r) - tx - 1;
    if (lp <= rp) {
      ql = lp;
      qr = rp;
      F = a[i].f;
      Query(1, 1, m);
    }
  }
  Erase(1, 1, m);
  Solve(l, mid);
  Solve(mid + 1, r);
}
int main() {
  read(n);
  read(K);
  for (int i = 1; i <= n; i++) a[i].read();
  sort(a + 1, a + n + 1);
  Solve(1, n);
  printf("%I64d\n", Ans);
  return 0;
}
