#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int inf = 1e9 + 50;
int n, k;
struct node {
  int x, r, q;
  bool friend operator<(node a, node b) { return a.r > b.r; }
} p[N];
struct tree {
  int l, r, sum;
} T[N * 60];
int tot;
void update(int &o, int l, int r, int p) {
  if (!o) o = ++tot;
  T[o].sum++;
  if (l == r) return;
  int mid = l + r >> 1;
  if (p <= mid)
    update(T[o].l, l, mid, p);
  else
    update(T[o].r, mid + 1, r, p);
}
int query(int o, int l, int r, int L, int R) {
  if (!o) return 0;
  if (L <= l && R >= r) return T[o].sum;
  int mid = l + r >> 1;
  int ret = 0;
  if (L <= mid) ret += query(T[o].l, l, mid, L, R);
  if (R > mid) ret += query(T[o].r, mid + 1, r, L, R);
  return ret;
}
map<int, int> mp;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d%d%d", &p[i].x, &p[i].r, &p[i].q);
  sort(p + 1, p + 1 + n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = p[i].q - k; j <= p[i].q + k; j++) {
      ans +=
          (long long)query(mp[j], -inf, inf, p[i].x - p[i].r, p[i].x + p[i].r);
    }
    update(mp[p[i].q], -inf, inf, p[i].x);
  }
  printf("%lld\n", ans);
  return 0;
}
