#include <bits/stdc++.h>
using namespace std;
const long long maxn = 300005;
long long tree[2][maxn];
void add(long long k, long long v, long long t) {
  for (long long i = k; i < maxn; i += i & -i) tree[t][i] += v;
}
long long qry(long long k, long long t) {
  long long res = 0;
  for (long long i = k; i >= 1; i -= i & -i) res += tree[t][i];
  return res;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  for (long long j = 0; j < maxn; j++) tree[0][j] = tree[1][j] = 0;
  long long n, k;
  cin >> n >> k;
  vector<pair<long long, long long>> vec[10005];
  set<long long> s;
  for (long long i = 0; i < n; i++) {
    long long x, r, f;
    cin >> x >> r >> f;
    vec[f].push_back({r, x});
    s.insert(x);
    s.insert(x + r);
    s.insert(x - r);
  }
  for (long long i = 0; i < 10005; i++)
    if (!vec[i].empty()) sort(vec[i].begin(), vec[i].end());
  map<long long, long long> g;
  vector<long long> v(s.begin(), s.end());
  for (long long i = 0; i < v.size(); i++) g[v[i]] = i + 1;
  long long sum1 = 0;
  long long sum2 = 0;
  for (long long i = 0; i < 10005; i++) {
    if (vec[i].empty()) continue;
    vector<pair<long long, long long>> other;
    for (long long j = max((long long)0, i - k); j < i; j++) {
      for (pair<long long, long long> p : vec[j]) other.push_back(p);
    }
    long long pOther = -1;
    if (!other.empty()) {
      sort(other.begin(), other.end());
      pOther = other.size() - 1;
    }
    long long pCur = vec[i].size() - 1;
    long long cont1 = 0;
    long long cont2 = 0;
    vector<pair<long long, long long>> undo;
    while (pOther != -1 || pCur != -1) {
      if (pOther == -1 ||
          (pCur != -1 && vec[i][pCur].first >= other[pOther].first)) {
        long long x = vec[i][pCur].second;
        long long l = x - vec[i][pCur].first;
        long long r = x + vec[i][pCur].first;
        cont1 += qry(g[r], 0) - qry(g[l] - 1, 0);
        cont2 += qry(g[r], 1) - qry(g[l] - 1, 1);
        add(g[x], 1, 0);
        undo.push_back({g[x], 0});
        pCur--;
      } else {
        long long x = other[pOther].second;
        long long l = x - other[pOther].first;
        long long r = x + other[pOther].first;
        cont2 += qry(g[r], 0) - qry(g[l] - 1, 0);
        add(g[x], 1, 1);
        undo.push_back({g[x], 1});
        pOther--;
      }
    }
    for (pair<long long, long long> p : undo) add(p.first, -1, p.second);
    sum1 += cont1;
    sum2 += cont2;
  }
  cout << sum1 + sum2 << '\n';
}
