#include <bits/stdc++.h>
using namespace std;
int N, K, x[100010], r[100010], q[100010], pret[100010], sled[100010], hshidx;
long long bit[5 * 100010], rez;
vector<int> iq[100010];
vector<pair<int, int> > tr;
set<int> sQ, sX;
map<int, int> hQ, rhQ, hX;
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    pq;
priority_queue<pair<int, int> > pq1;
bool cmp(pair<int, int> a, pair<int, int> b) { return x[a.first] < x[b.first]; }
void update(int x, int val) {
  for (; x < 5 * 100010; x += x & -x) bit[x] += val;
}
long long query(int l, int r) {
  long long sum = 0;
  for (; r > 0; r -= r & -r) sum += bit[r];
  for (l--; l > 0; l -= l & -l) sum -= bit[l];
  return sum;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> x[i] >> r[i] >> q[i];
    sX.insert(x[i]);
    sX.insert(x[i] + r[i]);
    sX.insert(x[i] - r[i]);
    sQ.insert(q[i]);
  }
  hshidx = 0;
  for (auto x : sX) hX[x] = ++hshidx;
  hshidx = 0;
  for (auto q : sQ) hQ[q] = ++hshidx, rhQ[hshidx] = q;
  for (int i = 1; i <= N; i++) {
    iq[hQ[q[i]]].push_back(i);
    pret[i] = hX[x[i] - r[i]];
    sled[i] = hX[x[i] + r[i]];
    x[i] = hX[x[i]];
  }
  for (int i = 1; i <= int((sQ).size()); i++) {
    for (int j = i; j <= int((sQ).size()) && rhQ[j] <= rhQ[i] + K; j++) {
      for (auto x : iq[j]) tr.push_back({x, (i == j)});
    }
    sort(begin(tr), end(tr), cmp);
    for (auto cur : tr) {
      int idx = cur.first;
      if (cur.second) {
        while (!pq.empty() && pq.top().first < x[idx])
          update(pq.top().second, -1), pq.pop();
        rez += query(pret[idx], x[idx]);
      }
      update(x[idx], 1);
      pq.push({sled[idx], x[idx]});
    }
    while (!pq.empty()) update(pq.top().second, -1), pq.pop();
    for (int k = int((tr).size()) - 1; k >= 0; k--) {
      pair<int, int> cur = tr[k];
      int idx = cur.first;
      if (cur.second) {
        while (!pq1.empty() && pq1.top().first > x[idx])
          update(pq1.top().second, -1), pq1.pop();
        rez += query(x[idx], sled[idx]);
      } else {
        update(x[idx], 1);
        pq1.push({pret[idx], x[idx]});
      }
    }
    while (!pq1.empty()) update(pq1.top().second, -1), pq1.pop();
    tr.clear();
  }
  cout << rez;
  return 0;
}
