#include <bits/stdc++.h>
using namespace std;
inline int getidx(const vector<int>& ar, int x) {
  return lower_bound(ar.begin(), ar.end(), x) - ar.begin();
}
inline long long GCD(long long a, long long b) {
  long long n;
  if (a < b) swap(a, b);
  while (b != 0) {
    n = a % b;
    a = b;
    b = n;
  }
  return a;
}
inline long long LCM(long long a, long long b) {
  if (a == 0 || b == 0) return GCD(a, b);
  return a / GCD(a, b) * b;
}
inline long long CEIL(long long n, long long d) {
  return n / d + (long long)(n % d != 0);
}
inline long long ROUND(long long n, long long d) {
  return n / d + (long long)((n % d) * 2 >= d);
}
inline long long POW(long long a, long long n) {
  if (n < 0) return 0;
  long long ret = 1;
  while (n) {
    if (n % 2) ret *= a;
    a = a * a;
    n /= 2;
  }
  return ret;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
const int MAXN = 1e5, MAXF = 1e4;
struct fenwick {
  vector<int> t;
  void init(int n) { t = vector<int>(n + 1, 0); }
  void add(int u, int x) {
    for (++u; u < t.size(); u += (u & -u)) t[u] += x;
  }
  int sum(int u) {
    int ret = 0;
    for (++u; u > 0; u -= (u & -u)) ret += t[u];
    return ret;
  }
} ft;
int n, k;
vector<pair<int, int> > tar, fr[MAXF];
vector<int> xs;
void input() {
  cin >> n >> k;
  for (int(i) = (0); (i) < (int)(n); ++(i)) {
    int x, r, f;
    cin >> x >> r >> f;
    --f;
    for (int(d) = (-1); (d) < (int)(2); ++(d)) xs.push_back(x + d * r);
    fr[f].push_back({x, r});
  }
}
int solve() {
  sort(xs.begin(), xs.end()),
      (xs).resize(unique(xs.begin(), xs.end()) - (xs).begin());
  for (int(i) = (0); (i) < (int)(MAXF); ++(i))
    sort(fr[i].begin(), fr[i].end(), [](pair<int, int>& u, pair<int, int>& v) {
      return u.first < v.first;
    });
  long long ans = 0;
  ft.init(xs.size());
  for (int(f) = (0); (f) < (int)(MAXF); ++(f)) {
    if (fr[f].size()) {
      tar.clear();
      for (int(j) = (max(0, f - k)); (j) < (int)(min(MAXF, f + k + 1)); ++(j))
        for (int(l) = (0); (l) < (int)(fr[j].size()); ++(l))
          tar.push_back(fr[j][l]);
      sort(tar.begin(), tar.end(), [](pair<int, int>& u, pair<int, int>& v) {
        return u.first - u.second < v.first - v.second;
      });
      int p = 0;
      for (int(i) = (0); (i) < (int)(fr[f].size()); ++(i)) {
        int cx = fr[f][i].first, crx = fr[f][i].first + fr[f][i].second;
        cx = getidx(xs, cx), crx = getidx(xs, crx);
        while (p < tar.size() &&
               getidx(xs, tar[p].first - tar[p].second) <= cx) {
          ft.add(getidx(xs, tar[p++].first), 1);
        }
        ans += ft.sum(crx) - ft.sum(cx);
      }
      p = 0;
      for (int(i) = (0); (i) < (int)(fr[f].size()); ++(i)) {
        int cx = fr[f][i].first, crx = fr[f][i].first + fr[f][i].second;
        cx = getidx(xs, cx), crx = getidx(xs, crx);
        while (p < tar.size() &&
               getidx(xs, tar[p].first - tar[p].second) <= cx) {
          ft.add(getidx(xs, tar[p++].first), -1);
        }
      }
      42;
    }
  }
  cout << ans << '\n';
  return 0;
}
void execute() { input(), solve(); }
int main(void) {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  execute();
  return 0;
}
