#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long large = 2000000000000000000LL;
vector<long long> sum;
int up;
long long uv;
void update(int o, int l, int r) {
  if (l == r) {
    sum[o] += uv;
  } else {
    int m = (l + r) / 2;
    if (up <= m) update(o * 2, l, m);
    if (up > m) update(o * 2 + 1, m + 1, r);
    sum[o] = sum[o * 2] + sum[o * 2 + 1];
  }
}
int ql, qr;
long long re;
void query(int o, int l, int r) {
  if (qr < ql) return;
  if (ql <= l && qr >= r) {
    re += sum[o];
  } else {
    int m = (l + r) / 2;
    if (ql <= m) query(o * 2, l, m);
    if (qr > m) query(o * 2 + 1, m + 1, r);
  }
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> x(n, 0), r = x, q = x;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &x[i], &r[i], &q[i]);
  }
  vector<int> uni;
  for (int i = 0; i < n; i++) uni.push_back(q[i]);
  sort(uni.begin(), uni.end());
  uni.erase(unique(uni.begin(), uni.end()), uni.end());
  int sz = (int)uni.size();
  map<int, int> comp;
  for (int i = 0; i < sz; i++) comp[uni[i]] = i;
  vector<vector<int> > adj(sz, vector<int>());
  for (int i = 0; i < n; i++) adj[comp[q[i]]].push_back(i);
  long long ans = 0;
  for (int i = 0; i < sz; i++) {
    int Q = uni[i];
    int y = upper_bound(uni.begin(), uni.end(), uni[i] - k - 1) - uni.begin();
    vector<int> uni(1, -1000000010);
    vector<pair<pair<int, int>, int> > eve;
    for (int j = y; j <= i; j++) {
      for (int k = 0; k < (int)adj[j].size(); k++) {
        int id = adj[j][k];
        eve.push_back(
            pair<pair<int, int>, int>(pair<int, int>(x[id] - r[id], 1), id));
        eve.push_back(pair<pair<int, int>, int>(
            pair<int, int>(x[id] + r[id] + 1, 0), id));
        uni.push_back(x[id]);
      }
    }
    for (int j = 0; j < (int)adj[i].size(); j++) {
      int id = adj[i][j];
      eve.push_back(pair<pair<int, int>, int>(pair<int, int>(x[id], 2), id));
    }
    sort(eve.begin(), eve.end());
    uni.push_back(2000000010);
    sort(uni.begin(), uni.end());
    uni.erase(unique(uni.begin(), uni.end()), uni.end());
    map<int, int> comp;
    for (int j = 0; j < (int)uni.size(); j++) comp[uni[j]] = j;
    int sz = (int)uni.size();
    sum.assign(4 * sz + 10, 0);
    long long temp = 0;
    for (int j = 0; j < (int)eve.size(); j++) {
      int ty = eve[j].first.second;
      int id = eve[j].second;
      if (ty == 1) {
        up = comp[x[id]];
        uv = 1;
        if (q[id] == Q) uv = 1000000000;
        update(1, 0, sz - 1);
      }
      if (ty == 0) {
        up = comp[x[id]];
        uv = -1;
        if (q[id] == Q) uv = -1000000000;
        update(1, 0, sz - 1);
      }
      if (ty == 2) {
        ql = upper_bound(uni.begin(), uni.end(), x[id] - r[id] - 1) -
             uni.begin();
        qr = upper_bound(uni.begin(), uni.end(), x[id] + r[id]) - uni.begin();
        qr--;
        re = 0;
        query(1, 0, sz - 1);
        ans += re % 1000000000LL;
        temp += re / 1000000000LL;
      }
    }
    temp -= (long long)adj[i].size();
    temp /= 2;
    ans += temp;
  }
  cout << ans << endl;
  return 0;
}
