#include <bits/stdc++.h>
static const int N = 1 << 17;
struct Fenwick {
  int n;
  int t[N * 4];
  void set(int n) { this->n = n; }
  void add(int x, int v) {
    for (int i = x; i <= n; i += (i & -i)) t[i] += v;
  }
  void clear(int x) {
    for (int i = x; i <= n; i += (i & -i)) t[i] = 0;
  }
  int query(int x) {
    int ans = 0;
    for (int i = x; i > 0; i -= (i & -i)) ans += t[i];
    return ans;
  }
  int query(int l, int r) { return query(r) - query(l - 1); }
} fenwick;
int readInt() {
  int x = 0, f = 1;
  int c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
struct node {
  long long x, R, l, r, q;
  bool operator<(node const& o) const { return R < o.R; }
} s[N];
bool cmp(const node& a, const node& b) { return a.q < b.q; }
long long ans;
int n, k, cc;
long long d[N * 4];
void merge(int l, int r) {
  if (l == r) return;
  int m = (l + r) >> 1;
  merge(l, m);
  merge(m + 1, r);
  int st = m + 1, e = m;
  for (int i = l; i <= m; ++i) {
    while (e < r && s[e + 1].q <= s[i].q + k) {
      ++e;
      fenwick.add(s[e].x, +1);
    }
    while (st <= e && s[st].q < s[i].q - k) {
      fenwick.add(s[st].x, -1);
      ++st;
    }
    ans += fenwick.query(s[i].l, s[i].r);
  }
  for (int i = m + 1; i <= r; ++i) fenwick.clear(s[i].x);
  std::inplace_merge(s + l, s + m + 1, s + r + 1, cmp);
}
int solve() {
  n = readInt();
  k = readInt();
  cc = 0;
  for (int i = 1; i <= n; ++i) {
    s[i].x = readInt();
    s[i].R = readInt();
    s[i].q = readInt();
    d[cc++] = s[i].x;
    d[cc++] = s[i].x - s[i].R;
    d[cc++] = s[i].x + s[i].R;
  }
  std::sort(d, d + cc);
  cc = std::unique(d, d + cc) - d;
  for (int i = 1; i <= n; ++i) {
    s[i].l = std::lower_bound(d, d + cc, s[i].x - s[i].R) - d;
    s[i].r = std::lower_bound(d, d + cc, s[i].x + s[i].R) - d;
    s[i].x = std::lower_bound(d, d + cc, s[i].x) - d;
    ++s[i].l;
    ++s[i].r;
    ++s[i].x;
  }
  std::sort(s + 1, s + n + 1);
  fenwick.set(cc);
  ans = 0;
  merge(1, n);
  printf("%lld\n", ans);
  return 0;
}
int main(int argc, char* argv[]) { solve(); }
