#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
const int inf = (int)1e9 + 1;
const long long big = (long long)1e18 + 1;
const int P = 31;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MAX_INT = (1 << 31) - 1;
const double eps = 1e-6;
const double pi = atan2(0, -1);
struct radio {
  int x, r, f;
  radio() {}
  void read() { scanf("%d %d %d", &x, &r, &f); }
};
bool comp(radio r1, radio r2) { return r1.r > r2.r; }
struct node {
  int x, y;
  node *L, *R;
  int sz;
  node() {}
  node(int x, int y) {
    this->x = x;
    this->y = y;
    L = NULL;
    R = NULL;
    sz = 1;
  }
};
int get_sz(node* t) {
  if (t == NULL)
    return 0;
  else
    return t->sz;
}
void upd_sz(node* t) {
  if (t == NULL)
    return;
  else
    t->sz = get_sz(t->L) + get_sz(t->R) + 1;
}
bool exist(node* t, int x) {
  if (t == NULL)
    return false;
  else if (t->x == x)
    return true;
  else if (x < t->x)
    return exist(t->L, x);
  else
    return exist(t->R, x);
}
pair<node*, node*> split(node* t, int x) {
  if (t == NULL)
    return {NULL, NULL};
  else if (x <= t->x) {
    auto p = split(t->L, x);
    t->L = p.second;
    p.second = t;
    upd_sz(p.first);
    upd_sz(p.second);
    return p;
  } else {
    auto p = split(t->R, x);
    t->R = p.first;
    p.first = t;
    upd_sz(p.first);
    upd_sz(p.second);
    return p;
  }
}
node* merge(node* l, node* r) {
  if (l == NULL)
    return r;
  else if (r == NULL)
    return l;
  else if (l->y <= r->y) {
    l->R = merge(l->R, r);
    upd_sz(l);
    return l;
  } else {
    r->L = merge(l, r->L);
    upd_sz(r);
    return r;
  }
}
node* add(node* t, int x, int y) {
  if (exist(t, x)) return t;
  node* _new = new node(x, y);
  auto p = split(t, x);
  return merge(merge(p.first, _new), p.second);
}
node* del(node* t, int x) {
  auto p = split(t, x);
  auto p1 = split(p.second, x + 1);
  return merge(p.first, p1.second);
}
int next(node* t, int x) {
  if (t->x <= x && t->R == NULL)
    return inf;
  else if (t->x <= x)
    return next(t->R, x);
  else if (t->L != NULL)
    return min(next(t->L, x), t->x);
  else
    return t->x;
}
int prev(node* t, int x) {
  if (t->x >= x && t->L == NULL)
    return -inf;
  else if (t->x >= x)
    return prev(t->L, x);
  else if (t->R != NULL)
    return max(prev(t->R, x), t->x);
  else
    return t->x;
}
int kth(node* t, int k) {
  if (get_sz(t->L) == k)
    return t->x;
  else if (get_sz(t->L) > k)
    return kth(t->L, k);
  else
    return kth(t->R, k - get_sz(t->L) - 1);
}
int get_cnt(node* t, int l, int r) {
  auto p = split(t, l);
  auto p1 = split(p.second, r);
  int cnt = get_sz(p1.first);
  t = merge(p.first, merge(p1.first, p1.second));
  return cnt;
}
node* DD[10000 + 1];
int main() {
  srand(time(0));
  int n, k;
  scanf("%d %d", &n, &k);
  vector<radio> a(n);
  for (int i = 0; i < n; i++) a[i].read();
  sort(a.begin(), a.end(), comp);
  long long ans = 0;
  for (auto i : a) {
    for (int j = max(1, i.f - k); j <= min(10000, i.f + k); j++)
      ans += get_cnt(DD[j], i.x - i.r, i.x + i.r + 1);
    DD[i.f] = add(DD[i.f], i.x, (rand() << 15) | rand());
  }
  printf("%lld\n", ans);
  return 0;
}
