#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
struct node {
  long long key, sz;
  int prior;
  node *l;
  node *r;
  node(long long _key) {
    prior = rand();
    key = _key;
    sz = 1;
    l = r = 0;
  }
};
typedef node *pnode;
long long getsz(pnode now) {
  if (!now) return 0;
  return now->sz;
}
void fixsz(pnode now) {
  if (!now) return;
  now->sz = getsz(now->l) + getsz(now->r) + 1;
  return;
}
void split(pnode now, long long key, pnode &l, pnode &r) {
  if (!now) {
    l = r = 0;
  } else if (now->key <= key) {
    split(now->r, key, now->r, r), l = now;
  } else {
    split(now->l, key, l, now->l), r = now;
  }
  fixsz(now);
}
void merge(pnode &now, pnode l, pnode r) {
  if (!l) {
    now = r;
  } else if (!r) {
    now = l;
  } else if (l->prior > r->prior) {
    merge(l->r, l->r, r), now = l;
  } else {
    merge(r->l, l, r->l), now = r;
  }
  fixsz(now);
}
void insert(pnode &now, pnode it) {
  if (!now) {
    now = it;
  } else if (it->prior > now->prior) {
    split(now, it->key, it->l, it->r), now = it;
  } else if (it->key > now->key) {
    insert(now->r, it);
  } else {
    insert(now->l, it);
  }
  fixsz(now);
}
void erase(pnode &now, long long key) {
  if (!now) return;
  if (key > now->key) {
    erase(now->r, key);
  } else if (key < now->key) {
    erase(now->l, key);
  } else {
    merge(now, now->l, now->r);
  }
  fixsz(now);
}
long long cnt(pnode now, long long x) {
  if (!now) return 0;
  if (x > now->key) {
    return getsz(now->l) + 1 + cnt(now->r, x);
  } else {
    return cnt(now->l, x);
  }
}
long long cent(pnode now, long long x) {
  if (!now) return 0;
  if (x < now->key) {
    return getsz(now->r) + 1 + cent(now->l, x);
  } else {
    return cent(now->r, x);
  }
}
const int MAXN = 1e5 + 100;
long long res;
long long n, k;
map<int, int> freq;
vector<pair<int, pair<int, int> > > v;
int nyak;
vector<pair<int, pair<int, int> > > anggota[MAXN], hasil[MAXN];
pnode root;
bool cmp(pair<int, pair<int, int> > a, pair<int, pair<int, int> > b) {
  if (a.second.first == b.second.first) {
    if (a.second.second == b.second.second) return a.first > b.first;
    return a.second.second > b.second.second;
  }
  return a.second.first > b.second.first;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int x, r, first;
    cin >> x >> r >> first;
    v.push_back(make_pair(x, make_pair(r, first)));
    if (!freq.count(first)) {
      freq[first] = nyak++;
    }
  }
  for (int i = 0; i < n; i++) {
    pair<int, pair<int, int> > now = v[i];
    for (int j = v[i].second.second - k; j <= v[i].second.second + k; j++) {
      if (!freq.count(j)) continue;
      int sek = freq[j];
      hasil[sek].push_back(now);
    }
    anggota[freq[now.second.second]].push_back(now);
  }
  for (int i = 0; i < nyak; i++) {
    root = NULL;
    int kiri = 0;
    sort(hasil[i].begin(), hasil[i].end(), cmp);
    sort(anggota[i].begin(), anggota[i].end(), cmp);
    for (int j = 0; j < hasil[i].size(); j++) {
      pair<int, pair<int, int> > now = hasil[i][j];
      while ((kiri < anggota[i].size()) &&
             ((anggota[i][kiri].second.first > now.second.first) ||
              (anggota[i][kiri].second.first == now.second.first &&
               anggota[i][kiri].second.second > now.second.second) ||
              (anggota[i][kiri].second.first == now.second.first &&
               anggota[i][kiri].second.second == now.second.second &&
               anggota[i][kiri].first > now.first))) {
        insert(root, new node(anggota[i][kiri++].first));
      }
      res = res + getsz(root) - cnt(root, now.first - now.second.first) -
            cent(root, now.first + now.second.first);
    }
  }
  cout << res << '\n';
}
