#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
const int E = 1e9;
const int T = N * 30;
int n, m;
long long ans;
int tot, val[T], L[T], R[T], tr[N], rk[N];
struct Node {
  int x, r, f;
  bool operator<(const Node &d) const { return x < d.x; }
} a[N];
bool cmp(int i, int j) { return a[i].x + a[i].r < a[j].x + a[j].r; }
void ins(int &p, int l, int r, int x, int z) {
  if (!p) p = ++tot;
  val[p] += z;
  if (l == r) return;
  if (x <= ((l + r) >> 1))
    ins(L[p], l, ((l + r) >> 1), x, z);
  else
    ins(R[p], ((l + r) >> 1) + 1, r, x, z);
}
int qry(int p, int l, int r, int x) {
  if (!p) return 0;
  if (x <= l) return val[p];
  if (x <= ((l + r) >> 1))
    return qry(L[p], l, ((l + r) >> 1), x) + val[R[p]];
  else
    return qry(R[p], ((l + r) >> 1) + 1, r, x);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].f), rk[i] = i;
  sort(a + 1, a + n + 1);
  sort(rk + 1, rk + n + 1, cmp);
  for (int i = 1, j = 1; i <= n; i++) {
    while (j <= n && a[rk[j]].x + a[rk[j]].r < a[i].x)
      ins(tr[a[rk[j]].f], 1, E, a[rk[j]].x, -1), j++;
    for (int k = max(a[i].f - m, 1); k <= min(10000, a[i].f + m); k++)
      ans += qry(tr[k], 1, E, max(1, a[i].x - a[i].r));
    ins(tr[a[i].f], 1, E, a[i].x, 1);
  }
  printf("%lld", ans);
}
