#include <bits/stdc++.h>
using namespace std;
struct robot {
  int x, r, q;
  bool operator<(const robot &a) const { return r > a.r; }
} a[101010];
map<int, int> rt;
struct Node {
  int lch, rch, cnt;
  Node() { lch = rch = cnt = 0; }
} nd[10101010];
int nn = 0;
void modify(int &rt1, int l, int r, int v) {
  if (!rt1) rt1 = ++nn;
  nd[rt1].cnt++;
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (v <= mid)
    modify(nd[rt1].lch, l, mid, v);
  else
    modify(nd[rt1].rch, mid + 1, r, v);
}
int query(int rt1, int l, int r, int ql, int qr) {
  if (rt1 == 0) return 0;
  if (ql <= l && qr >= r) return nd[rt1].cnt;
  int mid = (l + r) >> 1, ret = 0;
  if (ql <= mid) ret += query(nd[rt1].lch, l, mid, ql, qr);
  if (mid < qr) ret += query(nd[rt1].rch, mid + 1, r, ql, qr);
  return ret;
}
int main() {
  int n, k;
  long long ans = 0;
  scanf("%d%d", &n, &k);
  rt.clear();
  for (int i = 1; i <= n; i++) scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q);
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++) {
    for (int j = a[i].q - k; j <= a[i].q + k; j++)
      if (rt.count(j))
        ans += query(rt[j], 0, 1000000000, a[i].x - a[i].r, a[i].x + a[i].r);
    modify(rt[a[i].q], 0, 1000000000, a[i].x);
  }
  printf("%lld", ans);
  return 0;
}
