#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100010, LN = 17;
int t[4 * N * LN];
int L[4 * N * LN], R[4 * N * LN];
int node;
int build(int l, int r) {
  int i = node++;
  assert(i < 4 * N * LN);
  if (l == r) {
    t[i] = 0;
  } else {
    int m = (l + r) / 2;
    L[i] = build(l, m);
    R[i] = build(m + 1, r);
    t[i] = t[L[i]] + t[R[i]];
  }
  return i;
}
int add(int i, int l, int r, int p, int v) {
  int j = node++;
  assert(j < 4 * N * LN);
  if (l == r) {
    t[j] = v;
  } else {
    int m = (l + r) / 2;
    if (p <= m) {
      L[j] = add(L[i], l, m, p, v);
      R[j] = R[i];
    } else {
      L[j] = L[i];
      R[j] = add(R[i], m + 1, r, p, v);
    }
    t[j] = t[L[j]] + t[R[j]];
  }
  return j;
}
int query(int i, int l, int r, int ql, int qr) {
  if (r < ql || l > qr) return 0;
  if (ql <= l && r <= qr) return t[i];
  int m = (l + r) / 2;
  return query(L[i], l, m, ql, qr) + query(R[i], m + 1, r, ql, qr);
}
vector<int> compr;
int x[N], r[N], f[N];
int root[10010];
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", x + i, r + i, f + i);
    compr.push_back(x[i]);
  }
  sort(begin(compr), end(compr));
  compr.resize(unique(begin(compr), end(compr)) - begin(compr));
  vector<pair<int, int>> order;
  for (int i = 0; i < n; i++) {
    order.emplace_back(x[i], i);
    x[i] = lower_bound(begin(compr), end(compr), x[i]) - begin(compr);
  }
  sort(begin(order), end(order));
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  assert(build(0, compr.size() - 1) == 0);
  ll ans = 0;
  for (auto& p : order) {
    while (pq.size() && p.first > pq.top().first) {
      int j = pq.top().second;
      int tmp = root[f[j]];
      int rep = add(tmp, 0, compr.size() - 1, x[j], 0);
      root[f[j]] = rep;
      pq.pop();
    }
    int i = p.second;
    int e = lower_bound(begin(compr), end(compr), compr[x[i]] - r[i]) -
            begin(compr);
    for (int w = max(f[i] - k, 1); w <= min(f[i] + k, 10000); w++) {
      assert(e >= 0 and e < compr.size());
      ans += query(root[w], 0, compr.size() - 1, e, x[i]);
    }
    int tmp = root[f[i]];
    int rep = add(tmp, 0, compr.size() - 1, x[i], 1);
    root[f[i]] = rep;
    pq.emplace(compr[x[i]] + r[i], i);
  }
  printf("%lld\n", ans);
  return 0;
}
