#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  long long int ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T>
inline T bpow(T p, T e) {
  long long int ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p);
    p = (p * p);
  }
  return (T)ret;
}
int ts, kk = 1;
struct D {
  D *l, *r;
  int cnt;
  D() {
    l = r = NULL;
    cnt = 0;
  }
};
int n, m;
pair<int, pair<int, int> > a[100005];
D *tr[10005];
void updt(D *rt, int s, int e, int p) {
  if (rt == NULL) return;
  if (p <= s && e <= p) {
    rt->cnt++;
    return;
  }
  if (rt->l == NULL) rt->l = new D();
  if (rt->r == NULL) rt->r = new D();
  if (p <= ((s + e) >> 1))
    updt(rt->l, s, ((s + e) >> 1), p);
  else
    updt(rt->r, ((s + e) >> 1) + 1, e, p);
  rt->cnt++;
}
int qry(D *c, int s, int e, int st, int ed) {
  if (c == NULL) return 0;
  if (st <= s && e <= ed) return c->cnt;
  if (ed <= ((s + e) >> 1)) return qry(c->l, s, ((s + e) >> 1), st, ed);
  if (((s + e) >> 1) < st) return qry(c->r, ((s + e) >> 1) + 1, e, st, ed);
  return qry(c->l, s, ((s + e) >> 1), st, ed) +
         qry(c->r, ((s + e) >> 1) + 1, e, st, ed);
}
int main() {
  int t, i, j, k;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m + 1; i++) tr[i] = NULL;
  for (i = 0; i < n; i++) {
    scanf("%d%d%d", &j, &k, &t);
    a[i] = make_pair(-k, pair<int, int>(t, j));
  }
  sort(a, a + n);
  long long int rs = 0;
  for (i = 0; i < n; i++) {
    int l, r;
    l = a[i].second.second + a[i].first;
    r = a[i].second.second - a[i].first;
    if (l < 1) l = 1;
    if (r > 1000000007LL) r = 1000000007LL;
    k = a[i].second.first;
    for (j = max(1, k - m); j <= min(10005, k + m); j++)
      rs += qry(tr[j], 1, 1000000007LL, l, r);
    if (tr[k] == NULL) tr[k] = new D();
    updt(tr[k], 1, 1000000007LL, a[i].second.second);
  }
  printf("%lld\n", rs);
  return 0;
}
