#include <bits/stdc++.h>
const long long md = 1e9 + 7;
const int Inf = 1e9;
const long long Inf64 = 1e18;
const long long MaxN = 2e5 + 10;
const long long MaxM = 11;
const long double eps = 1e-15;
const long long dx[4] = {0, 1, 0, -1};
const long long dy[4] = {1, 0, -1, 0};
const long long ddx[4] = {1, 1, -1, -1};
const long long ddy[4] = {1, -1, 1, -1};
const long double Pi = acos(-1);
using namespace std;
long long gcd(long long a, long long b) {
  while (a) {
    b %= a;
    swap(a, b);
  }
  return b;
}
long long gcdex(long long a, long long mod = md) {
  long long g = mod, r = a, x = 0, y = 1;
  while (r != 0) {
    long long q = g / r;
    g %= r;
    swap(g, r);
    x -= q * y;
    swap(x, y);
  }
  return x < 0 ? x + mod : x;
}
long long binpow(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) {
      res *= a;
      res %= md;
    }
    a *= a;
    a %= md;
    n >>= 1;
  }
  return res % md;
}
struct el {
  long long x, r, q;
};
vector<long long> pos;
el Rb[MaxN];
map<long long, long long> Tree[4 * MaxN];
long long calc(map<long long, long long>& Mp, long long lq, long long rq) {
  auto it = Mp.lower_bound(lq);
  long long res = 0;
  while (it != Mp.end() && it->first <= rq) {
    res += it->second;
    it++;
  }
  return res;
}
long long getSum(long long l, long long r, long long x, long long y,
                 long long lq, long long rq, long long id) {
  if (x <= l && r <= y) {
    return calc(Tree[id], lq, rq);
  }
  long long mid = (l + r) >> 1;
  long long res = 0;
  if (x <= mid) res += getSum(l, mid, x, y, lq, rq, 2 * id + 1);
  if (mid < y) res += getSum(mid + 1, r, x, y, lq, rq, 2 * id + 2);
  return res;
}
void insert(long long l, long long r, long long x, long long a, long long id) {
  Tree[id][a]++;
  if (l == r) {
    return;
  } else {
    long long mid = (l + r) / 2;
    if (mid >= x) {
      insert(l, mid, x, a, 2 * id + 1);
    } else {
      insert(mid + 1, r, x, a, 2 * id + 2);
    }
  }
}
long long N, K;
long long getL(long long x) {
  long long l = 0, r = pos.size() - 1;
  int res = -1;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (pos[mid] >= x) {
      res = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  return res;
}
long long getR(long long x) {
  long long l = 0, r = pos.size() - 1;
  long long res = -1;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (pos[mid] <= x) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  return res;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cerr.tie(0);
  cout << fixed, cout.precision(10);
  cin >> N >> K;
  pos.assign(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> Rb[i].x >> Rb[i].r >> Rb[i].q;
    pos[i] = Rb[i].x;
  }
  sort(pos.begin(), pos.end());
  long long m = unique(pos.begin(), pos.end()) - pos.begin();
  pos.resize(m);
  sort(Rb, Rb + N, [=](el a, el b) { return a.r > b.r; });
  long long Ans = 0;
  for (int i = 0; i < N; i++) {
    long long l = getL(Rb[i].x - Rb[i].r);
    long long r = getR(Rb[i].x + Rb[i].r);
    l = max(l, 0LL);
    r = min(r, (long long)pos.size() - 1);
    Ans += getSum(0, pos.size() - 1, l, r, Rb[i].q - K, Rb[i].q + K, 0);
    long long x = getR(Rb[i].x);
    insert(0, pos.size() - 1, x, Rb[i].q, 0);
  }
  cout << Ans;
  cerr << '\n'
       << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
       << '\n';
  return 0;
}
