#include <bits/stdc++.h>
using namespace std;
bool comp(array<int, 3> a, array<int, 3> b) { return a[1] > b[1]; }
const int SZ = (1 << 30);
template <class T>
struct node {
  vector<array<int, 2>> c;
  vector<int> val;
  int cur;
  node() {
    c.push_back({-1, -1});
    cur = 1;
    val.push_back(0);
  }
  void upd(int nd, int ind, T v, int L = 0, int R = SZ - 1) {
    if (L == ind && R == ind) {
      val[nd] += v;
      return;
    }
    int M = (L + R) / 2;
    if (ind <= M) {
      if (c[nd][0] == -1) {
        c[nd][0] = cur++;
        c.push_back({-1, -1});
        val.push_back(0);
      }
      upd(c[nd][0], ind, v, L, M);
    } else {
      if (c[nd][1] == -1) {
        c[nd][1] = cur++;
        c.push_back({-1, -1});
        val.push_back(0);
      }
      upd(c[nd][1], ind, v, M + 1, R);
    }
    val[nd] = 0;
    for (int i = 0; i < 2; ++i)
      if (c[nd][i] != -1) val[nd] += val[c[nd][i]];
  }
  T query(int nd, int lo, int hi, int L = 0, int R = SZ - 1) {
    if (hi < L || R < lo) return 0;
    if (lo <= L && R <= hi) return val[nd];
    int M = (L + R) / 2;
    T res = 0;
    if (c[nd][0] != -1) res += query(c[nd][0], lo, hi, L, M);
    if (c[nd][1] != -1) res += query(c[nd][1], lo, hi, M + 1, R);
    return res;
  }
};
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<array<int, 3>> a(n);
  for (int i = 0; i < n; ++i) {
    int x, r, q;
    cin >> x >> r >> q;
    a[i] = {x, r, q};
  }
  sort((a).begin(), (a).end(), comp);
  map<int, int> exist;
  map<int, node<int>> segs;
  long long ans = 0;
  for (auto [x, r, q] : a) {
    for (int i = q - k; i <= q + k; ++i) {
      if (!exist[i]) continue;
      ans += segs[i].query(0, max(x - r, 0), x + r);
    }
    if (!exist[q]) {
      segs[q] = node<int>();
    }
    segs[q].upd(0, x, 1);
    exist[q] = 1;
  }
  cout << ans << "\n";
}
