#include <bits/stdc++.h>
using namespace std;
struct node {
  long long x, r, f, xx, v, id;
} a[200100];
vector<node> b[10010];
long long n, k, i, ex[200100 * 2], cnt, size, root[200100], ls[200100 * 20],
    rs[200100 * 20], sum[200100 * 20], ans, j, tot;
bool cmp(node x, node y) { return x.x < y.x; }
void init() {
  sort(ex + 1, ex + 1 + cnt);
  size = unique(ex + 1, ex + 1 + cnt) - ex - 1;
  for (i = 1; i <= n; i++)
    a[i].xx = upper_bound(ex + 1, ex + 1 + size, a[i].x) - ex - 1,
    a[i].v = upper_bound(ex + 1, ex + 1 + size, a[i].x - a[i].r) - ex - 1;
}
long long find(long long f, long long x) {
  long long l = 0, r = (long long)b[f].size() - 1, ans = -1;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (b[f][mid].x <= x)
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return ans;
}
long long find1(long long f, long long x) {
  long long l = 0, r = (long long)b[f].size() - 1, ans = -1;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (b[f][mid].x >= x)
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return ans;
}
void insert(long long last, long long &rt, long long l, long long r,
            long long x) {
  rt = ++tot;
  ls[rt] = ls[last];
  rs[rt] = rs[last];
  if (l == r) {
    sum[rt] = sum[last] + 1;
    return;
  }
  long long mid = (l + r) / 2;
  if (x <= mid)
    insert(ls[last], ls[rt], l, mid, x);
  else
    insert(rs[last], rs[rt], mid + 1, r, x);
  sum[rt] = sum[ls[rt]] + sum[rs[rt]];
}
long long query(long long last, long long rt, long long l, long long r,
                long long x) {
  if (r <= x) return sum[rt] - sum[last];
  long long mid = (l + r) / 2;
  if (x <= mid)
    return query(ls[last], ls[rt], l, mid, x);
  else
    return sum[ls[rt]] - sum[ls[last]] + query(rs[last], rs[rt], mid + 1, r, x);
}
int main() {
  scanf("%d %d", &n, &k);
  for (i = 1; i <= n; i++) {
    scanf("%d %d %d", &a[i].x, &a[i].r, &a[i].f);
    ex[++cnt] = a[i].x - a[i].r;
    ex[++cnt] = a[i].x;
  }
  init();
  sort(a + 1, a + 1 + n, cmp);
  for (i = 1; i <= n; i++) a[i].id = i;
  for (i = 1; i <= n; i++) {
    long long tmp = b[a[i].f].size() ? root[b[a[i].f].back().id] : 0;
    insert(tmp, root[a[i].id], 1, size, a[i].v);
    b[a[i].f].push_back(a[i]);
  }
  for (i = 1; i <= n; i++) {
    for (j = max(1ll, a[i].f - k); j <= a[i].f + k; j++) {
      long long l = find1(j, a[i].x);
      long long r = find(j, a[i].x + a[i].r);
      if (l <= r && l != -1 && r != -1)
        if (l >= 1)
          ans +=
              query(root[b[j][l - 1].id], root[b[j][r].id], 1, size, a[i].xx);
        else
          ans += query(0, root[b[j][r].id], 1, size, a[i].xx);
    }
  }
  printf("%I64d\n", ans - n);
  return 0;
}
