#include <bits/stdc++.h>
using namespace std;
const int MXN = (int)3e6 + 10;
const int INF = (int)1e9 + 7;
const long long LINF = (long long)1e18 + 10;
const double EPS = (double)1e-6;
const double PI = (double)acos(-1.0);
struct rs {
  int first, r, f;
  rs() {}
} a[MXN];
int n, k;
vector<int> r_arr[21111];
vector<int> where[21111];
vector<pair<pair<int, int>, int> > query[21111];
long long ans;
struct segment_tree {
  vector<vector<int> > t;
  void resize(int n) { t.resize(4 * n); }
  void build(int ff, int v, int tl, int tr) {
    if (tl == tr) {
      t[v] = vector<int>(1, r_arr[ff][tl]);
    } else {
      int tm = (tl + tr) / 2;
      build(ff, v + v, tl, tm);
      build(ff, v + v + 1, tm + 1, tr);
      merge(t[v * 2].begin(), t[v * 2].end(), t[v * 2 + 1].begin(),
            t[v * 2 + 1].end(), back_inserter(t[v]));
    }
  }
  int get(int l, int r, int first, int v, int tl, int tr) {
    if (tr < l || r < tl || l > r) return 0;
    if (l <= tl && tr <= r) {
      int tmp = lower_bound(t[v].begin(), t[v].end(), first) - t[v].begin();
      return tr - tl + 1 - tmp;
    }
    int tm = (tl + tr) / 2;
    return get(l, r, first, v + v, tl, tm) +
           get(l, r, first, v + v + 1, tm + 1, tr);
  }
} Tree[21111];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d%d", &a[i].first, &a[i].r, &a[i].f);
  }
  sort(a + 1, a + n + 1, [](rs a, rs b) { return a.first < b.first; });
  for (int i = 1; i <= n; ++i) {
    int tmp_it;
    for (int j = max(1, a[i].f - k); j <= min(10010, a[i].f + k); ++j) {
      tmp_it =
          lower_bound(where[j].begin(), where[j].end(), a[i].first - a[i].r) -
          where[j].begin();
      if (tmp_it != (int)where[j].size())
        query[j].push_back(
            make_pair(make_pair(tmp_it, (int)r_arr[j].size() - 1), a[i].first));
    }
    where[a[i].f].push_back(a[i].first);
    r_arr[a[i].f].push_back(a[i].first + a[i].r);
  }
  for (int cur_f = 1; cur_f <= 10010; ++cur_f) {
    if (!(int)r_arr[cur_f].size()) continue;
    Tree[cur_f].resize((int)r_arr[cur_f].size());
    Tree[cur_f].build(cur_f, 1, 0, (int)r_arr[cur_f].size() - 1);
    for (int i = 0; i < (int)query[cur_f].size(); ++i) {
      ans += 1LL * Tree[cur_f].get(query[cur_f][i].first.first,
                                   query[cur_f][i].first.second,
                                   query[cur_f][i].second, 1, 0,
                                   (int)r_arr[cur_f].size() - 1);
    }
  }
  printf("%I64d", ans);
  return 0;
}
