#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int n, kk, tot;
int xi[maxn + 5], ri[maxn + 5], qi[maxn + 5], num[maxn + 5];
int id[maxn + 5], inv[maxn + 5];
int L[maxn + 5], R[maxn + 5];
int sq[maxn + 5];
int tr[maxn + 5];
void Add(int x, int d) {
  for (; x <= n; x += x & (-x)) tr[x] += d;
}
int Sum(int x) {
  int r = 0;
  for (; x; x -= x & (-x)) r += tr[x];
  return r;
}
bool cmp(const int &a, const int &b) {
  if (qi[a] != qi[b]) return qi[a] < qi[b];
  return xi[a] < xi[b];
}
bool cmp2(const int &a, const int &b) { return ri[a] < ri[b]; }
int main() {
  scanf("%d%d", &n, &kk);
  tot = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d%d", xi + i, ri + i, qi + i);
    num[i] = i;
    sq[++tot] = qi[i];
    L[tot] = 1 << 30, R[tot] = -L[tot];
  }
  sort(sq + 1, sq + 1 + tot);
  tot = unique(sq + 1, sq + 1 + tot) - sq - 1;
  sort(num + 1, num + 1 + n, cmp);
  for (int i = 1; i <= n; ++i) {
    id[num[i]] = i, inv[i] = num[i];
    int rk = lower_bound(sq + 1, sq + 1 + tot, qi[num[i]]) - sq;
    L[rk] = min(L[rk], i);
    R[rk] = max(R[rk], i);
    Add(i, 1);
  }
  sort(num + 1, num + 1 + n, cmp2);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    int lrk = lower_bound(sq + 1, sq + 1 + tot, qi[num[i]] - kk) - sq;
    int rrk = upper_bound(sq + 1, sq + 1 + tot, qi[num[i]] + kk) - sq - 1;
    Add(id[num[i]], -1);
    for (int k = lrk; k <= rrk; ++k) {
      int l = L[k], r = R[k], psl = r + 1;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (xi[num[i]] - ri[num[i]] <= xi[inv[mid]])
          psl = mid, r = mid - 1;
        else
          l = mid + 1;
      }
      l = L[k], r = R[k];
      int psr = l - 1;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (xi[num[i]] + ri[num[i]] >= xi[inv[mid]])
          psr = mid, l = mid + 1;
        else
          r = mid - 1;
      }
      if (psl > psr) continue;
      ans += Sum(psr) - Sum(psl - 1);
    }
  }
  printf("%lld\n", ans);
  return 0;
}
