#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int k;
map<int, int> mp;
int S;
long long res;
struct linetree {
  map<int, int> rot;
  int s[22 * N][2];
  int v[22 * N];
  int ct;
  inline void ins(int p, int l, int r, int pos) {
    v[p]++;
    if (r - l == 1) {
      return;
    }
    int mid = (l + r) / 2;
    if (pos <= mid)
      ins(s[p][0] = s[p][0] ? s[p][0] : ++ct, l, mid, pos);
    else
      ins(s[p][1] = s[p][1] ? s[p][1] : ++ct, mid, r, pos);
  }
  inline int qry(int p, int l, int r, int dl, int dr) {
    if ((dl == l && dr == r) || p == 0) {
      return v[p];
    }
    int mid = (l + r) / 2;
    int res = 0;
    if (dl < mid) res += qry(s[p][0], l, mid, dl, min(dr, mid));
    if (mid < dr) res += qry(s[p][1], mid, r, max(dl, mid), dr);
    return res;
  }
  inline void cins(int q, int x) {
    if (rot.count(q) == 0) rot[q] = ++ct;
    ins(rot[q], 0, S, x);
  }
  inline int cqr(int q, int dl, int dr) {
    if (rot.count(q) == 0) return 0;
    return qry(rot[q], 0, S, dl, dr);
  }
} lt;
struct data {
  int x;
  int r;
  int q;
  friend bool operator<(data a, data b) { return a.r > b.r; }
} a[N];
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);
  for (int i = 1; i <= n; i++) {
    mp[a[i].x] = 1;
    mp[a[i].x + a[i].r] = 1;
    mp[a[i].x - a[i].r] = 1;
  }
  map<int, int>::iterator it, it1;
  for (it = mp.begin(), it1 = it, ++it1; it1 != mp.end(); ++it, ++it1)
    it1->second += it->second;
  S = mp.size();
  for (int i = 1; i <= n; i++) {
    for (int j = a[i].q - k; j <= a[i].q + k; j++)
      res += lt.cqr(j, mp[a[i].x - a[i].r] - 1, mp[a[i].x + a[i].r]);
    lt.cins(a[i].q, mp[a[i].x]);
  }
  printf("%I64d", res);
  return 0;
}
