#include <bits/stdc++.h>
using namespace std;
template <typename T>
class fenwick_tree {
 private:
  int n;
  vector<T> fenw;

 public:
  fenwick_tree(int _n) : n(_n) { fenw.resize(n); }
  size_t size() { return n; }
  void update(int x, T val) {
    while (x < n) {
      fenw[x] += val;
      x |= (x + 1);
    }
  }
  void update(int x, int y, T val) {
    update(x, +val);
    update(y + 1, -val);
  }
  T query(int x) {
    T ans = 0;
    while (x >= 0) {
      ans += fenw[x];
      x = (x & (x + 1)) - 1;
    }
    return ans;
  }
};
struct station {
  int x, r, f;
};
int n, k, cur = 0;
vector<station> stations, sorted_stations;
map<int, int> mp;
fenwick_tree<int> fenw(0);
long long solve(int l, int r) {
  if (l == r) {
    return 0;
  }
  int mid = (l + r) / 2;
  long long ans = solve(l, mid) + solve(mid + 1, r);
  int i = l, j = l;
  for (int t = mid + 1; t <= r; t++) {
    while (j <= mid && stations[j].f <= stations[t].f + k) {
      fenw.update(mp[stations[j].x - stations[j].r],
                  mp[stations[j].x + stations[j].r], +1);
      j++;
    }
    while (i < j && stations[i].f < stations[t].f - k) {
      fenw.update(mp[stations[i].x - stations[i].r],
                  mp[stations[i].x + stations[i].r], -1);
      i++;
    }
    ans += fenw.query(mp[stations[t].x]);
  }
  for (int t = i; t < j; t++) {
    fenw.update(mp[stations[t].x - stations[t].r],
                mp[stations[t].x + stations[t].r], -1);
  }
  int t = l;
  i = l;
  j = mid + 1;
  while (i <= mid || j <= r) {
    if (i > mid) {
      sorted_stations[t++] = stations[j++];
    } else if (j > r) {
      sorted_stations[t++] = stations[i++];
    } else if (stations[i].f < stations[j].f) {
      sorted_stations[t++] = stations[i++];
    } else {
      sorted_stations[t++] = stations[j++];
    }
  }
  for (int t = l; t <= r; t++) {
    stations[t] = sorted_stations[t];
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  set<int> st;
  for (int i = 0; i < n; i++) {
    int x, r, f;
    cin >> x >> r >> f;
    stations.push_back({x, r, f});
    st.insert(x);
    st.insert(x - r);
    st.insert(x + r);
  }
  for (auto &x : st) {
    mp[x] = cur++;
  }
  sort(stations.begin(), stations.end(),
       [](const station &a, const station &b) { return a.r < b.r; });
  sorted_stations.resize(n);
  fenw = fenwick_tree<int>(cur);
  cout << solve(0, n - 1) << '\n';
  return 0;
}
