#include <bits/stdc++.h>
using namespace std;
const int _ = 3e5 + 5;
inline int read() {
  char ch = '!';
  int z = 1, num = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') z = -1, ch = getchar();
  while (ch <= '9' && ch >= '0')
    num = (num << 3) + (num << 1) + ch - '0', ch = getchar();
  return z * num;
}
int n, K, a[_];
struct ha {
  int x, r, q;
  bool operator<(const ha &A) const { return x < A.x; }
} t[_];
long long ans;
map<int, int> c[_];
struct Ha {
  int x, id;
  bool operator<(const Ha &A) const { return x > A.x; }
};
priority_queue<Ha> Q;
map<int, int>::iterator P;
void insert(int x, int op) {
  while (x <= a[0]) {
    c[x][op]++;
    x += (x & (-x));
  }
}
void del(int x, int op) {
  while (x <= a[0]) {
    c[x][op]--;
    x += (x & (-x));
  }
}
int qy(int x, int l, int r) {
  int s = 0;
  l = max(l, 0);
  while (x) {
    for (P = c[x].lower_bound(l); P != c[x].end() && P->first <= r; ++P)
      s += P->second;
    x -= (x & (-x));
  }
  return s;
}
int find(int x) {
  int k = lower_bound(a + 1, a + 1 + a[0], x) - a;
  return k;
}
int main() {
  n = read(), K = read();
  for (int i = 1; i <= n; ++i) {
    t[i].x = read(), t[i].r = read(), t[i].q = read();
    a[++a[0]] = t[i].x - t[i].r;
    a[++a[0]] = t[i].x + t[i].r;
    a[++a[0]] = t[i].x;
  }
  sort(t + 1, t + 1 + n);
  sort(a + 1, a + 1 + a[0]);
  a[0] = unique(a + 1, a + 1 + a[0]) - a - 1;
  for (int i = 1; i <= n; ++i) {
    while (!Q.empty() && Q.top().x < t[i].x)
      del(find(t[Q.top().id].x), t[Q.top().id].q), Q.pop();
    int l = find(t[i].x - t[i].r), r = find(t[i].x + t[i].r);
    ans += qy(r, t[i].q - K, t[i].q + K) - qy(l - 1, t[i].q - K, t[i].q + K);
    Q.push((Ha){t[i].x + t[i].r, i});
    insert(find(t[i].x), t[i].q);
  }
  printf("%lld\n", ans);
  return 0;
}
