#include <bits/stdc++.h>
using namespace std;
struct robot {
  int x, r, s;
  inline bool operator<(const robot &a) const { return x < a.x; }
};
struct node {
  int x;
  node *l, *r;
  node() {
    x = 0;
    l = r = 0;
  }
  node(int xx) {
    x = xx;
    l = r = 0;
  }
};
inline void upd(node *t, int Tl, int Tr, int x, int delta) {
  if (Tl > x || Tr < x) {
    return;
  }
  if (Tl == x && Tr == x) {
    t->x += delta;
    return;
  }
  int mid = (Tl + Tr) / 2;
  if (x <= mid) {
    if (!t->l) t->l = new node();
    upd(t->l, Tl, mid, x, delta);
  } else {
    if (!t->r) t->r = new node();
    upd(t->r, mid + 1, Tr, x, delta);
  }
  t->x = 0;
  if (t->l) t->x += t->l->x;
  if (t->r) t->x += t->r->x;
}
inline int get(node *t, int Tl, int Tr, int l, int r) {
  if (Tl > r || Tr < l) return 0;
  if (!t) return 0;
  if (l <= Tl && Tr <= r) return t->x;
  int mid = (Tl + Tr) / 2;
  return get(t->l, Tl, mid, l, r) + get(t->r, mid + 1, Tr, l, r);
}
const int N = 1e5 + 100;
robot a[N];
unordered_map<int, node *> c;
inline void upd(int s, int x, int val) {
  if (c[s] == nullptr) c[s] = new node();
  x = max(x, 0);
  x = min(x, (int)1e9);
  upd(c[s], 0, 1e9, x, val);
}
inline int get(int s, int x) {
  x = max(x, 0);
  x = min(x, (int)1e9);
  return get(c[s], 0, 1e9, x, 1e9);
}
inline int get(int s, int x, int r) { return get(s, x - r); }
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    int x, r, s;
    cin >> x >> r >> s;
    a[i] = {x, r, s};
  }
  sort(a, a + n);
  multiset<pair<int, pair<int, int> > > Set;
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    while (Set.size() && Set.begin()->first < a[i].x) {
      auto val = Set.begin()->second;
      upd(val.first, val.second, -1);
      Set.erase(Set.begin());
    }
    for (int j = a[i].s - k; j <= a[i].s + k; ++j) {
      ans += get(j, a[i].x, a[i].r);
    }
    Set.insert(make_pair(a[i].x + a[i].r, make_pair(a[i].s, a[i].x)));
    upd(a[i].s, a[i].x, 1);
  }
  cout << ans;
  return 0;
}
