#include <bits/stdc++.h>
using namespace std;
bool is_prime(long long n) {
  for (long long i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
long long getPow(long long a, long long b) {
  long long res = 1ll, tp = a;
  while (b) {
    if (b & 1ll) {
      res *= tp;
      res %= 1000000007;
    }
    tp *= tp;
    tp %= 1000000007;
    b >>= 1ll;
  }
  return res;
}
long long vec_mult(long long x1, long long y1, long long x2, long long y2,
                   long long x3, long long y3) {
  return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
}
void ok() {
  cout << "YES" << endl;
  exit(0);
}
void no() {
  cout << "NO" << endl;
  exit(0);
}
inline long long nxt() {
  long long x;
  cin >> x;
  return x;
}
struct Fenwick {
  int n;
  vector<int> a;
  Fenwick(int _n = 0) : n(_n), a(_n) {}
  void add(int pos, int x) {
    while (pos < n) {
      a[pos] += x;
      pos |= pos + 1;
    }
  }
  int get(int pos) {
    int res = 0;
    while (pos >= 0) {
      res += a[pos];
      pos = (pos & (pos + 1)) - 1;
    }
    return res;
  }
  int get(int l, int r) { return get(r) - get(l - 1); }
};
struct Robot {
  int x, r, iq;
  Robot() {}
  Robot(int t1, int t2, int t3) : x(t1), r(t2), iq(t3) {}
  bool operator<(const Robot& a) const { return r > a.r; }
};
const long long N = 2e5 + 5, inf = 1e18;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  long long n = nxt(), k = nxt();
  vector<Robot> mas(n);
  for (int i = 0; i < n; i++) {
    long long t1 = nxt(), t2 = nxt(), t3 = nxt();
    mas[i] = Robot(t1, t2, t3);
  }
  sort(mas.begin(), mas.end());
  vector<long long> iqs;
  for (int i = 0; i < n; i++) {
    iqs.push_back(mas[i].iq);
  }
  sort((iqs).begin(), (iqs).end());
  (iqs).resize(unique((iqs).begin(), (iqs).end()) - (iqs).begin());
  vector<vector<long long>> positions(iqs.size());
  for (int i = 0; i < n; i++) {
    int ind = lower_bound(iqs.begin(), iqs.end(), mas[i].iq) - iqs.begin();
    positions[ind].push_back(mas[i].x);
  }
  for (int i = 0; i < positions.size(); i++) {
    sort((positions[i]).begin(), (positions[i]).end());
    (positions[i])
        .resize(unique((positions[i]).begin(), (positions[i]).end()) -
                (positions[i]).begin());
  }
  vector<Fenwick> fenws;
  for (int i = 0; i < iqs.size(); i++) {
    fenws.push_back(Fenwick(positions[i].size()));
  }
  long long ans = 0;
  for (auto t : mas) {
    int l = lower_bound(iqs.begin(), iqs.end(), t.iq - k) - iqs.begin();
    while (l < iqs.size() && iqs[l] <= t.iq + k) {
      long long le =
          lower_bound(positions[l].begin(), positions[l].end(), t.x - t.r) -
          positions[l].begin();
      long long ri =
          upper_bound(positions[l].begin(), positions[l].end(), t.x + t.r) -
          positions[l].begin() - 1;
      ans += fenws[l].get(le, ri);
      l++;
    }
    int ind = lower_bound(iqs.begin(), iqs.end(), t.iq) - iqs.begin();
    int pos = lower_bound(positions[ind].begin(), positions[ind].end(), t.x) -
              positions[ind].begin();
    fenws[ind].add(pos, 1);
  }
  cout << ans;
  return 0;
}
