#include <bits/stdc++.h>
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int N = 1e5 + 7;
const int INF = 1e9 + 7;
int t[4 * N], tsp[4 * N];
int lf[N], rf[N], pos[N];
int n, k, sp;
long long ans;
struct rb {
  int x, r, iq;
} a[N];
vector<int> b;
bool comp(rb a, rb b) {
  if (a.iq == b.iq) return a.r < b.r;
  return a.iq < b.iq;
}
bool comp2(int i, int j) {
  if (i == 0) return true;
  if (j == 0) return false;
  return a[i].x < a[j].x;
}
bool comp3(int i, int j) {
  if (i == 0) return true;
  if (j == 0) return false;
  return a[i].r < a[j].r;
}
void build(int v, int l, int r) {
  if (l == r) {
    tsp[v] = 0;
    if (a[b[l]].iq == sp) tsp[v] = 1;
    t[v] = 1;
  } else {
    int mid = (l + r) >> 1;
    build(v + v, l, mid);
    build(v + v + 1, mid + 1, r);
    t[v] = t[v + v] + t[v + v + 1];
    tsp[v] = tsp[v + v] + tsp[v + v + 1];
  }
}
void upd(int v, int l, int r, int pos) {
  if (l == r)
    t[v] = tsp[v] = 0;
  else {
    int mid = (l + r) >> 1;
    if (pos <= mid)
      upd(v + v, l, mid, pos);
    else
      upd(v + v + 1, mid + 1, r, pos);
    t[v] = t[v + v] + t[v + v + 1];
    tsp[v] = tsp[v + v] + tsp[v + v + 1];
  }
}
int get1(int v, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) return t[v];
  if (r < ql || qr < l) return 0;
  int mid = (l + r) >> 1;
  return get1(v + v, l, mid, ql, qr) + get1(v + v + 1, mid + 1, r, ql, qr);
}
int get2(int v, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr) return tsp[v];
  if (r < ql || qr < l) return 0;
  int mid = (l + r) >> 1;
  return get2(v + v, l, mid, ql, qr) + get2(v + v + 1, mid + 1, r, ql, qr);
}
void solve(int l, int r) {
  b.push_back(0);
  int n = r - l + 1;
  for (int i = l; i <= r; i++) b.push_back(i);
  sort(b.begin(), b.end(), comp2);
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    pos[b[i]] = i;
    int lb = 0, rb = i;
    while (lb + 1 < rb) {
      int mid = (lb + rb) >> 1;
      if (a[b[i]].x - a[b[mid]].x <= a[b[i]].r)
        rb = mid;
      else
        lb = mid;
    }
    lf[b[i]] = rb;
    lb = i, rb = n + 1;
    while (lb + 1 < rb) {
      int mid = (lb + rb) >> 1;
      if (a[b[mid]].x - a[b[i]].x <= a[b[i]].r)
        lb = mid;
      else
        rb = mid;
    }
    rf[b[i]] = lb;
  }
  sort(b.begin(), b.end(), comp3);
  for (int i = 1; i <= n; i++) {
    upd(1, 1, n, pos[b[i]]);
    if (a[b[i]].iq == sp)
      ans += get1(1, 1, n, lf[b[i]], rf[b[i]]);
    else
      ans += get2(1, 1, n, lf[b[i]], rf[b[i]]);
  }
  b.clear();
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].iq);
  sort(a + 1, a + n + 1, comp);
  int l = 1;
  a[n + 1].iq = INF;
  for (int i = 1; i <= n; i++) {
    if (a[i + 1].iq - a[l].iq > k) {
      sp = a[l].iq;
      solve(l, i);
      int t = l;
      while (a[l].iq == a[t].iq) l++;
      i--;
    }
  }
  cout << ans;
}
