#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9, N = 1e5 + 10, M = 4e6 + 10;
int n, k, maxNode;
struct data {
  int x, r, q;
} a[N];
struct node {
  int ls, rs, sum;
} t[M];
map<int, int> idx;
bool cmp(data i, data j) { return i.r > j.r; };
int qry(int x, int l, int r, int u, int v) {
  if (!x) return 0;
  if (u <= l && r <= v) return t[x].sum;
  int mid = (l + r) / 2;
  int ans = 0;
  if (u <= mid) ans += qry(t[x].ls, l, mid, u, v);
  if (v > mid) ans += qry(t[x].rs, mid + 1, r, u, v);
  return ans;
}
void upd(int &x, int l, int r, int rx) {
  if (!x) x = ++maxNode;
  t[x].sum++;
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (rx <= mid)
    upd(t[x].ls, l, mid, rx);
  else
    upd(t[x].rs, mid + 1, r, rx);
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = (1); i <= (n); ++i) {
    scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].q);
  }
  sort(a + 1, a + n + 1, cmp);
  long long ans = 0ll;
  for (int i = (1); i <= (n); ++i) {
    for (int j = (a[i].q - k); j <= (a[i].q + k); ++j)
      ans += qry(idx[j], -inf, inf, a[i].x - a[i].r, a[i].x + a[i].r);
    upd(idx[a[i].q], -inf, inf, a[i].x);
  }
  printf("%I64d\n", ans);
  return 0;
}
