#include <bits/stdc++.h>
using namespace std;
const int L = 0;
const int R = 1000000000;
struct node {
  node *lc, *rc;
  int val;
  node(node *x = NULL, node *y = NULL, int v = 0) { lc = x, rc = y, val = v; }
  inline void create(node *&x) {
    if (!x) x = new node();
  }
  inline int sum(node *x) { return (x) ? (x->val) : (0); }
  inline void insert(int l, int r, int value) {
    if (l == r) {
      val = val + 1;
      return;
    }
    int mid = (l + r) >> 1;
    if (mid >= value) {
      create(lc);
      lc->insert(l, mid, value);
    } else {
      create(rc);
      rc->insert(mid + 1, r, value);
    }
    val = sum(lc) + sum(rc);
  }
  inline int query(int l, int r, int qs, int qe) {
    if (l > qe || r < qs) {
      return 0;
    }
    if (l >= qs && r <= qe) {
      return sum(this);
    }
    int mid = (l + r) >> 1;
    int res = 0;
    if (lc) {
      res += lc->query(l, mid, qs, qe);
    }
    if (rc) {
      res += rc->query(mid + 1, r, qs, qe);
    }
    return res;
  }
};
struct data {
  int x, r, q;
  data(int xx = 0, int rr = 0, int qq = 0) {
    x = xx;
    r = rr;
    q = qq;
  }
  inline friend bool operator<(data a, data b) { return a.r > b.r; }
};
int n, k;
vector<data> arr;
map<int, node *> trees;
inline void update(data item) {
  if (trees[item.q] == NULL) {
    trees[item.q] = new node();
  }
  trees[item.q]->insert(L, R, item.x);
}
inline int query(int id, int pos, int rad) {
  if (!trees[id]) {
    return 0;
  }
  int res = trees[id]->query(L, R, max(L, pos - rad), min(R, pos + rad));
  return res;
}
inline int solve(data item) {
  int res = 0;
  for (int i = max(L, item.q - k); i <= min(R, item.q + k); i++) {
    res += query(i, item.x, item.r);
  }
  return res;
}
int main() {
  ios ::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int x, r, q;
    cin >> x >> r >> q;
    arr.push_back(data(x, r, q));
  }
  sort(arr.begin(), arr.end());
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += solve(arr[i]);
    update(arr[i]);
  }
  cout << ans << endl;
}
