#include <bits/stdc++.h>
using namespace std;
const int MAX = 3e5 + 5;
struct p {
  int x, id;
  bool operator<(const p &a) const { return x < a.x; }
} A[MAX];
int n, k, cnt, ID, Mr;
long long ans;
int s[MAX], nL[MAX], nR[MAX], nX[MAX], L[MAX], R[MAX], X[MAX], q[MAX], _[MAX],
    id[MAX], _id[MAX], a[MAX], b[MAX];
bool cmp(int a, int b) { return _[a] < _[b]; }
bool cmp1(int a, int b) { return q[a] < q[b]; }
int lowbit(int x) { return x & -x; }
void change(int x, int d) {
  for (int i = x; i <= ID; i += lowbit(i)) s[i] += d;
}
int ask(int x) {
  int ans = 0;
  for (int i = x; i; i -= lowbit(i)) ans += s[i];
  return ans;
}
int ask(int l, int r) { return ask(r) - ask(l - 1); }
int read() {
  int x(0);
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    ;
  for (; isdigit(ch); x = x * 10 + ch - 48, ch = getchar())
    ;
  return x;
}
void Solve(int l = 1, int r = n) {
  if (l == r) return;
  int mid(l + r >> 1), _L = mid + 1, _R = mid + 1;
  Solve(l, mid), Solve(mid + 1, r);
  for (int i = l; i <= r; ++i) id[i] = _id[i];
  for (int i = l; i <= mid; ++i) {
    while (_L <= r && q[id[_L]] < q[id[i]] - k) change(X[id[_L]], -1), ++_L;
    while (_R <= r && q[id[_R]] <= q[id[i]] + k) change(X[id[_R]], 1), ++_R;
    ans += ask(L[id[i]], R[id[i]]);
  }
  while (_L < _R) change(X[id[_L]], -1), ++_L;
  for (int i = l, L = l, R = mid + 1; i <= r; ++i) {
    if (L <= mid && (q[id[L]] <= q[id[R]] || R > r))
      _id[i] = id[L], ++L;
    else if (R <= r && (q[id[L]] > q[id[R]] || L > mid))
      _id[i] = id[R], ++R;
  }
}
int main() {
  n = read(), k = read();
  for (int i = 1; i <= n; ++i)
    _id[i] = i, nX[i] = read(), Mr = max(Mr, _[i] = read()), q[i] = read(),
    A[++cnt] = (p){nX[i], i}, A[++cnt] = (p){nL[i] = max(nX[i] - _[i], 0), i},
    A[++cnt] = (p){nR[i] = nX[i] + _[i], i};
  sort(A + 1, A + 1 + cnt), A[0].x = -1;
  for (int i = 1; i <= cnt; ++i) {
    if (A[i].x != A[i - 1].x) ++ID;
    if (nX[A[i].id] == A[i].x) X[A[i].id] = ID;
    if (nL[A[i].id] == A[i].x) L[A[i].id] = ID;
    if (nR[A[i].id] == A[i].x) R[A[i].id] = ID;
  }
  return sort(_id + 1, _id + 1 + n, cmp), Solve(), printf("%I64d", ans), 0;
}
