#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 3e5 + 5, L = 1e4 + 5;
struct node {
  int x, l, r, R, f;
};
bool cmp(const node &a, const node &b) { return (a.x < b.x); }
vector<node> nds, RM[M];
unordered_map<int, int> D[L];
int R(int x) { return (M - x - 2); }
void add(int ind, int d, int i) {
  for (ind = R(ind) + 1; ind < M; ind += (ind & (-ind))) {
    D[i][ind] += d;
    if (!D[i].count(ind)) D[i].erase(ind);
  }
}
int get(int ind, int i) {
  int sm = 0;
  for (ind = R(ind) + 1; ind; ind -= (ind & (-ind)))
    if (D[i].count(ind)) sm += D[i][ind];
  return (sm);
}
long long rt;
int n, k, l;
vector<int> v;
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < L; i++) D[i].max_load_factor(.13);
  for (int i = 0; i < n; i++) {
    int x, r, f;
    scanf("%d %d %d", &x, &r, &f);
    v.push_back(x);
    v.push_back(x - r);
    v.push_back(x + r);
    node nd = {x, x - r, x + r, r, f};
    nds.push_back(nd);
  }
  set<int> st(v.begin(), v.end());
  v.assign(st.begin(), st.end());
  for (node &nd : nds) {
    nd.x = lower_bound(v.begin(), v.end(), nd.x) - v.begin() + 1;
    nd.l = lower_bound(v.begin(), v.end(), nd.l) - v.begin() + 1;
    nd.r = lower_bound(v.begin(), v.end(), nd.r) - v.begin() + 1;
  }
  sort(nds.begin(), nds.end(), cmp);
  for (node nd : nds) {
    for (int i = l; i < nd.x; i++)
      for (node x : RM[i]) add(x.x, -1, x.f);
    l = nd.x;
    for (int i = max(1, nd.f - k); i <= min(L - 1, nd.f + k); i++)
      rt += get(nd.l, i);
    add(nd.x, 1, nd.f);
    RM[nd.r].push_back(nd);
  }
  printf("%lld", rt);
}
