#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int ls[N << 2], rs[N << 2], rt[N << 2], sum[N << 2];
int rcnt;
void update(int &rt, int l, int r, int pos) {
  if (rt == 0) rt = ++rcnt;
  if (l == r) {
    sum[rt]++;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    update(ls[rt], l, mid, pos);
  } else {
    update(rs[rt], mid + 1, r, pos);
  }
  sum[rt] = sum[ls[rt]] + sum[rs[rt]];
}
int query(int &rt, int l, int r, int ql, int qr) {
  if (rt == 0) return 0;
  if (ql <= l && qr >= r) return sum[rt];
  int mid = (l + r) >> 1, res = 0;
  if (ql <= mid) res += query(ls[rt], l, mid, ql, qr);
  if (qr > mid) res += query(rs[rt], mid + 1, r, ql, qr);
  return res;
}
int n, k;
struct node {
  int x, r, f;
  bool operator<(const node &z) const { return r > z.r; }
} a[N];
const int low = 1000000000;
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d %d", &a[i].x, &a[i].r, &a[i].f);
  }
  sort(a + 1, a + n + 1);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = a[i].f - k; j <= a[i].f + k; ++j) {
      if (j < 1 || j > 10000) continue;
      ans += query(rt[j], 1, low, max(1, a[i].x - a[i].r),
                   min(low, a[i].x + a[i].r));
    }
    update(rt[a[i].f], 1, low, a[i].x);
  }
  cout << ans << endl;
}
