#include <bits/stdc++.h>
using namespace std;
const long long int N1 = 1000010;
const long long int N2 = 2000010;
const long long int mod1 = 1e2 + 7;
const long long int mod2 = 1e9 + 7;
const int N = 1e5 + 5;
int Lft[40 * N], Rgt[40 * N], Root[40 * N], T[40 * N], k;
int Id[N], X[N], R[N], Frq[N], n, Now;
bool cmp(int x, int y) { return R[x] > R[y]; }
void updt(int &Nod, int lo, int hi, int x) {
  if (!Nod) Nod = ++Now;
  T[Nod]++;
  if (lo == hi) return;
  int mid = (lo + hi) >> 1;
  if (x <= mid)
    updt(Lft[Nod], lo, mid, x);
  else
    updt(Rgt[Nod], mid + 1, hi, x);
}
int Qry(int Nod, int lo, int hi, int l, int r) {
  if (!Nod || (hi < l || lo > r || lo > hi)) return 0;
  if (lo >= l && hi <= r) return T[Nod];
  int mid = (lo + hi) >> 1;
  int Ret = 0;
  Ret += Qry(Lft[Nod], lo, mid, l, r);
  Ret += Qry(Rgt[Nod], mid + 1, hi, l, r);
  return Ret;
}
int main() {
  cin >> n >> k;
  ;
  for (int i = 1; i <= n; i++) {
    cin >> X[i] >> R[i] >> Frq[i];
    ;
    Id[i] = i;
  }
  sort(Id + 1, Id + n + 1, cmp);
  long long int Res = 0;
  for (int i = 1; i <= n; i++) {
    int nw = Id[i];
    for (int j = max(1, Frq[nw] - k); j <= min(10000, Frq[nw] + k); j++) {
      Res += Qry(Root[j], 1, (int)1e9, max(1, X[nw] - R[nw]),
                 min((int)1e9, X[nw] + R[nw]));
    }
    updt(Root[Frq[nw]], 1, (int)1e9, X[nw]);
  }
  cout << Res << "\n";
  ;
  return 0;
}
