#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > f[10001];
int fn[2][200001], n, k;
vector<int> id;
inline void ad(int i, int x, int fn[]) {
  while (i <= n << 1) fn[i] += x, i += i & -i;
}
inline int gt(int i, int fn[]) {
  int an = 0;
  while (i) an += fn[i], i ^= i & -i;
  return an;
}
inline int gtid(int x) {
  return lower_bound(id.begin(), id.end(), x) - id.begin() + 1;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < (int)n; ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    f[c].push_back(make_pair(a, b));
    id.push_back(a);
    id.push_back(a - b - 1);
  }
  sort(id.begin(), id.end());
  for (int i = 1; i < (int)10001; ++i) sort(f[i].begin(), f[i].end());
  long long an = 0;
  for (int i = 1; i < (int)10001; ++i)
    if (!f[i].empty())
      for (int j = 0; j < (int)k + 1; ++j)
        if (i + j <= 10000 && !f[i + j].empty()) {
          set<pair<int, int> > d[2];
          vector<pair<int, int> > v;
          for (int z = 0; z < (int)f[i].size(); ++z)
            v.push_back(make_pair(f[i][z].first, z));
          for (int z = 0; z < (int)f[i + j].size(); ++z)
            v.push_back(make_pair(f[i + j][z].first, f[i].size() + z));
          sort(v.begin(), v.end());
          long long kk = 0;
          for (int z = 0; z < (int)v.size(); ++z) {
            int o = v[z].second >= f[i].size();
            if (o) v[z].second -= f[i].size();
            while (!d[o ^ 1].empty() && d[o ^ 1].begin()->first < v[z].first) {
              int t = d[o ^ 1].begin()->second;
              ad(gtid(f[o ? i : i + j][t].first), -1, fn[o ^ 1]);
              d[o ^ 1].erase(d[o ^ 1].begin());
            }
            kk +=
                d[o ^ 1].size() -
                gt(gtid(v[z].first - 1 - f[o ? j + i : i][v[z].second].second),
                   fn[o ^ 1]);
            d[o].insert(
                make_pair(v[z].first + f[o ? j + i : i][v[z].second].second,
                          v[z].second));
            ad(gtid(v[z].first), 1, fn[o]);
            if (o) v[z].second += f[i].size();
          }
          for (int o = 0; o < (int)2; ++o)
            while (!d[o].empty()) {
              int t = d[o].begin()->second;
              ad(gtid(f[o ? i + j : i][t].first), -1, fn[o]);
              d[o].erase(d[o].begin());
            }
          if (j == 0) kk -= f[i].size(), kk >>= 1;
          an += kk;
        }
  printf("%lld\n", an);
}
