#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
struct Node {
  Node *l, *r;
  int key, subtree, priority;
  inline Node() { this->l = this->r = 0; }
  inline Node(int key) {
    this->l = this->r = 0;
    this->key = key;
    this->subtree = 1;
    this->priority = (rand() << 16) ^ rand();
  }
  inline void update() {
    this->subtree = 1;
    if (l) this->subtree += l->subtree;
    if (r) this->subtree += r->subtree;
  }
};
struct Treap {
  struct Node* root;
  Treap() { this->root = 0; }
  void Merge(Node*& cur, Node* l, Node* r) {
    if (!l or !r)
      cur = l ? l : r;
    else if (l->priority > r->priority) {
      Merge(l->r, l->r, r);
      cur = l;
    } else {
      Merge(r->l, l, r->l);
      cur = r;
    }
    if (cur) cur->update();
  }
  void Split(Node* cur, Node*& l, Node*& r, int key) {
    if (!cur)
      l = r = 0;
    else if (key <= cur->key) {
      Split(cur->l, l, cur->l, key);
      r = cur;
    } else {
      Split(cur->r, cur->r, r, key);
      l = cur;
    }
    if (cur) cur->update();
  }
  void Erase(Node*& cur, int key) {
    if (!cur) return;
    if (cur->key == key)
      Merge(cur, cur->l, cur->r);
    else
      Erase((cur->key > key) ? cur->l : cur->r, key);
    if (cur) cur->update();
  }
  void Insert(Node*& cur, Node* it) {
    if (!cur)
      cur = it;
    else if (it->priority > cur->priority) {
      Split(cur, it->l, it->r, it->key);
      cur = it;
    } else {
      Insert((it->key < cur->key) ? cur->l : cur->r, it);
    }
    if (cur) cur->update();
  }
  inline void Insert(int key) {
    Node* temp = new Node(key);
    Insert(root, temp);
  }
  inline void Erase(int key) { Erase(root, key); }
  inline long long findans(int key) {
    long long ret = 0;
    Node* cur = root;
    while (cur) {
      if (cur->key <= key) ret++;
      if (key < cur->key) {
        cur = cur->l;
      } else {
        if (cur->l) ret += cur->l->subtree;
        cur = cur->r;
      }
    }
    return ret;
  }
};
int n, k;
vector<pair<int, pair<int, int> > > stations;
Treap treap[10004];
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    int x, r, f;
    scanf("%d %d %d", &x, &r, &f);
    treap[f].Insert(x);
    stations.push_back(make_pair(r, make_pair(x, f)));
  }
  sort(stations.begin(), stations.end());
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int x, r, f;
    r = stations[i].first;
    x = stations[i].second.first;
    f = stations[i].second.second;
    for (int ff = max(0, f - k); ff <= min(10000, f + k); ff++) {
      ans += treap[ff].findans(x + r) - treap[ff].findans(x - r - 1);
    }
    treap[f].Erase(x);
  }
  ans -= n;
  printf("%lld\n", ans);
  return 0;
}
