#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7, INF = 0x3f3f3f3f;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long qpow(long long a, long long n) {
  long long r = 1 % P;
  for (a %= P; n; a = a * a % P, n >>= 1)
    if (n & 1) r = r * a % P;
  return r;
}
long long inv(long long first) {
  return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P;
}
inline int rd() {
  int first = 0;
  char p = getchar();
  while (p < '0' || p > '9') p = getchar();
  while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar();
  return first;
}
const int N = 1e6 + 10;
int n, k, tot, b[N];
struct _ {
  int type, first, second;
  void pr() { printf("tp=%d,x=%d,y=%d\n", type, first, second); }
  bool operator<(const _& rhs) const {
    if (first != rhs.first) return first < rhs.first;
    if (second != rhs.second) return second < rhs.second;
    return type < rhs.type;
  }
} e[N];
struct __ {
  int first, r, q;
} a[N];
long long ans;
int c[N], tim[N], clk;
void add(int first) {
  for (; first <= *b; first += first & -first)
    tim[first] == clk ? ++c[first] : c[first] = 1, tim[first] = clk;
}
int qry(int first) {
  int r = 0;
  for (; first; first ^= first & -first) tim[first] == clk ? r += c[first] : 0;
  return r;
}
void merge(int l, int r) {
  if (l == r) return;
  merge(l, ((l + r) >> 1)), merge(((l + r) >> 1) + 1, r);
  int now = l;
  ++clk;
  for (int i = ((l + r) >> 1) + 1; i <= r; ++i) {
    while (now <= ((l + r) >> 1) && e[now].first <= e[i].first) {
      if (e[now].type == 0) add(e[now].second);
      ++now;
    }
    if (e[i].type == 1) {
      ans += qry(e[i].second);
    } else if (e[i].type == 2)
      ans -= qry(e[i].second);
  }
  inplace_merge(e + l, e + ((l + r) >> 1) + 1, e + r + 1);
}
int id(int first) { return lower_bound(b + 1, b + 1 + *b, first) - b; }
void add(int first, int second) {
  second = id(second);
  e[++tot] = {0, first, second};
}
void qry(int x1, int y1, int x2, int y2) {
  y1 = id(y1), y2 = id(y2);
  e[++tot] = {1, x2, y2};
  e[++tot] = {1, x1 - 1, y1 - 1};
  e[++tot] = {2, x1 - 1, y2};
  e[++tot] = {2, x2, y1 - 1};
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d%d", &a[i].first, &a[i].r, &a[i].q);
    b[++*b] = a[i].q, b[++*b] = a[i].q + k, b[++*b] = a[i].q - k - 1;
  }
  sort(a + 1, a + 1 + n, [](__ a, __ b) { return a.r > b.r; });
  sort(b + 1, b + 1 + *b), *b = unique(b + 1, b + 1 + *b) - b - 1;
  for (int i = 1; i <= n; ++i) {
    qry(a[i].first - a[i].r, a[i].q - k, a[i].first + a[i].r, a[i].q + k);
    add(a[i].first, a[i].q);
  }
  merge(1, tot);
  printf("%lld\n", ans);
}
