#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
std::vector<std::vector<int> > freq(10001);
class stree {
 public:
  std::vector<long long> t;
  int s;
  void build(std::vector<long long>& a, int v, int tl, int tr) {
    if (v == 1) {
      t.resize(a.size() * 4);
      s = a.size();
    }
    if (tl == tr)
      t[v] = a[tl];
    else {
      int tm = (tl + tr) / 2;
      build(a, v * 2, tl, tm);
      build(a, v * 2 + 1, tm + 1, tr);
      t[v] = t[v * 2] + t[v * 2 + 1];
    }
  }
  void set(int v, int tl, int tr, int pos, long long val) {
    if (pos == tl && tr == pos) {
      t[v] = val;
    } else {
      int tm = (tl + tr) / 2;
      if (pos <= tm)
        set(v * 2, tl, tm, pos, val);
      else
        set(v * 2 + 1, tm + 1, tr, pos, val);
      t[v] = t[v * 2] + t[v * 2 + 1];
    }
  }
  long long get(int v, int tl, int tr, int l, int r) {
    if (l > r) return 0;
    if (tl == l && tr == r) return t[v];
    int tm = (tl + tr) / 2;
    return get(v * 2, tl, tm, l, min(r, tm)) +
           get(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
  }
};
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  std::vector<int> x(n), r(n), f(n);
  map<int, int> xmap;
  std::vector<int> xord;
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &x[i], &r[i], &f[i]);
    freq[f[i]].push_back(i);
    xord.push_back(x[i]);
  }
  sort((xord).begin(), (xord).end());
  std::vector<int> lx(n);
  std::vector<int> rx(n);
  for (int i = 0; i < n; i++) {
    rx[i] = (upper_bound((xord).begin(), (xord).end(), x[i] + r[i]) -
             xord.begin()) -
            1;
    lx[i] =
        (lower_bound((xord).begin(), (xord).end(), x[i] - r[i]) - xord.begin());
  }
  for (int i = 0; i < n; i++) xmap[xord[i]] = i;
  std::vector<int> indmap(n);
  for (int i = 0; i < n; i++) indmap[i] = xmap[x[i]];
  set<std::pair<int, int> > proc;
  int cur = 0;
  for (auto u : freq[1]) proc.insert(make_pair(x[u], u));
  long long ans = 0;
  std::vector<long long> def(n, 0);
  stree st;
  st.build(def, 1, 0, n - 1);
  long long A = 1e6;
  while (1) {
    if (cur - k > 0)
      for (auto u : freq[cur - k]) proc.erase(make_pair(x[u], u));
    cur++;
    if (cur > 10000) break;
    for (auto u : freq[cur]) proc.insert(make_pair(x[u], u));
    if (freq[cur].empty()) continue;
    set<std::pair<int, int> > rend;
    for (auto u : proc) {
      int curx = x[u.second];
      while (!rend.empty() && rend.begin()->first < curx) {
        st.set(1, 0, n - 1, indmap[rend.begin()->second], 0);
        rend.erase(rend.begin());
      }
      if (f[u.second] == cur) {
        ans += st.get(1, 0, n - 1, lx[u.second], indmap[u.second]) % A;
        st.set(1, 0, n - 1, indmap[u.second], A + 1);
      } else {
        ans += st.get(1, 0, n - 1, lx[u.second], indmap[u.second]) / A;
        st.set(1, 0, n - 1, indmap[u.second], 1);
      }
      rend.insert(make_pair(curx + r[u.second], u.second));
    }
    for (auto u : rend) {
      st.set(1, 0, n - 1, indmap[u.second], 0);
    }
  }
  cout << ans;
}
