#include <bits/stdc++.h>
using namespace std;
const int N = 110000;
const int maxL = 1000000000;
const int maxF = 10000;
int pos[N], rad[N], f[N], id[N];
int root[N], cnt[N * 30], ls[N * 30], rs[N * 30];
int tot = 0;
void add(int pos, int l, int r, int &id) {
  if (!id) id = ++tot;
  cnt[id]++;
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    add(pos, l, mid, ls[id]);
  else
    add(pos, mid + 1, r, rs[id]);
}
int query(int lx, int rx, int l, int r, int id) {
  if (!id) return 0;
  if (lx <= l && r <= rx) return cnt[id];
  int mid = (l + r) >> 1;
  int ans = 0;
  if (lx <= mid) ans += query(lx, rx, l, mid, ls[id]);
  if (rx > mid) ans += query(lx, rx, mid + 1, r, rs[id]);
  return ans;
}
bool cmp(int x, int y) { return rad[x] > rad[y]; }
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) scanf("%d%d%d", pos + i, rad + i, f + i);
  for (int i = 1; i <= n; i++) id[i] = i;
  sort(id + 1, id + n + 1, cmp);
  long long ans = 0;
  memset(root, 0, sizeof(root));
  memset(cnt, 0, sizeof(cnt));
  memset(ls, 0, sizeof(ls));
  memset(rs, 0, sizeof(rs));
  for (int i = 1; i <= n; i++) {
    int x = id[i];
    for (int ff = max(f[x] - k, 1); ff <= min(f[x] + k, maxF); ff++) {
      ans += query(max(pos[x] - rad[x], 1), min(pos[x] + rad[x], maxL), 1, maxL,
                   root[ff]);
    }
    add(pos[x], 1, maxL, root[f[x]]);
  }
  cout << ans << endl;
}
