#include <bits/stdc++.h>
using namespace std;
const int N = 100002, M = 10002;
int n, k;
struct station {
  int x, r, first;
} a[N];
struct item {
  int key, prior, cnt = 1;
  item *l, *r;
  item() {}
  item(int key, int prior) : key(key), prior(prior), l(NULL), r(NULL) {}
};
typedef item* pitem;
pitem roots[M];
int cnt(pitem t) { return t ? t->cnt : 0; }
void upd_cnt(pitem t) {
  if (t) t->cnt = 1 + cnt(t->l) + cnt(t->r);
}
void split(pitem t, int key, pitem& l, pitem& r) {
  if (!t)
    l = r = NULL;
  else if (key < t->key)
    split(t->l, key, l, t->l), r = t;
  else
    split(t->r, key, t->r, r), l = t;
  upd_cnt(t);
}
void insert(pitem& t, pitem it) {
  if (!t)
    t = it;
  else if (it->prior > t->prior)
    split(t, it->key, it->l, it->r), t = it;
  else
    insert(it->key < t->key ? t->l : t->r, it);
  upd_cnt(t);
}
void merge(pitem& t, pitem l, pitem r) {
  if (!l || !r)
    t = l ? l : r;
  else if (l->prior > r->prior)
    merge(l->r, l->r, r), t = l;
  else
    merge(r->l, l, r->l), t = r;
  upd_cnt(t);
}
void printTreap(pitem x) {
  if (!x) return;
  printTreap(x->l);
  printf("%d ", x->key);
  printTreap(x->r);
}
int countLessThan(pitem root, int key) {
  if (root == NULL) return 0;
  if (root->key >= key) return countLessThan(root->l, key);
  return cnt(root->l) + 1 + countLessThan(root->r, key);
}
int main() {
  srand(time(NULL));
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    int x, r, first;
    scanf("%d%d%d", &x, &r, &first);
    a[i] = {x, r, first};
  }
  sort(a + 1, a + 1 + n,
       [](station first, station second) { return first.r > second.r; });
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = max(1, a[i].first - k); j <= min(a[i].first + k, M - 1); ++j)
      ans += countLessThan(roots[j], a[i].x + a[i].r + 1) -
             countLessThan(roots[j], a[i].x - a[i].r);
    insert(roots[a[i].first], new item(a[i].x, rand()));
  }
  cout << ans;
}
