#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int x[maxn];
int r[maxn];
int q[maxn];
int n, k;
int cid = 0;
int lpos[maxn], rpos[maxn];
int* tree[maxn];
vector<int> pos[maxn];
set<int> ayy;
map<int, int> lmao;
int lwb[maxn];
int pps[maxn];
int tot[maxn];
void add(int* t, int len, int pos, int d) {
  while (pos <= len) {
    t[pos] += d;
    pos += pos & -pos;
  }
}
int get(int* t, int r) {
  if (r == 0) return 0;
  int ans = 0;
  while (r) {
    ans += t[r];
    r -= r & -r;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  vector<pair<int, pair<int, int> > > kek;
  for (auto i = 0; i < n; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    kek.push_back(make_pair(a, make_pair(b, c)));
  }
  sort((kek).begin(), (kek).end());
  for (auto i = 0; i < n; ++i) {
    x[i] = kek[i].first;
    r[i] = kek[i].second.first;
    q[i] = kek[i].second.second;
    ayy.insert(q[i]);
  }
  for (auto c : ayy) {
    lmao[c] = cid++;
  }
  for (auto c : ayy) {
    int l = c - k, r = c + k;
    auto lt = ayy.lower_bound(l);
    auto rt = ayy.upper_bound(r);
    int i = lmao[c];
    if (rt == ayy.begin()) {
      rpos[i] = -1;
      continue;
    }
    --rt;
    int lp = *lt, rp = *rt;
    if (lp > rp) {
      rpos[i] = -1;
      continue;
    }
    lpos[i] = lmao[lp];
    rpos[i] = lmao[rp];
  }
  for (auto i = 0; i < n; ++i) {
    int p = lmao[q[i]];
    pps[i] = p;
    if (!pos[p].empty() && pos[p].back() == x[i]) continue;
    pos[p].push_back(x[i]);
  }
  for (auto i = 0; i < cid; ++i) {
    int sz = pos[i].size();
    tree[i] = new int[sz + 1];
    fill(tree[i], tree[i] + 1 + sz, 0);
  }
  set<pair<int, int> > s;
  long long ans = 0;
  for (auto i = 0; i < n; ++i) {
    while (!s.empty() && s.begin()->first < x[i]) {
      int p = pps[s.begin()->second];
      --tot[p];
      add(tree[p], pos[p].size(), lwb[s.begin()->second] + 1, -1);
      s.erase(s.begin());
    }
    int p = pps[i];
    for (auto j = lpos[p]; j < rpos[p] + 1; ++j) {
      int huy = lower_bound((pos[j]).begin(), (pos[j]).end(), x[i] - r[i]) -
                pos[j].begin();
      ans += tot[j] - get(tree[j], huy);
    }
    int j =
        lower_bound((pos[p]).begin(), (pos[p]).end(), x[i]) - pos[p].begin();
    lwb[i] = j;
    add(tree[p], pos[p].size(), j + 1, 1);
    ++tot[p];
    s.insert(make_pair(x[i] + r[i], i));
  }
  cout << ans;
}
