#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
const double PI = atan(1) * 4;
const int oo = 1000000000;
int n, k, l, r;
vector<pair<int, int> > v[10001];
vector<int> P[10001], M[10001];
vector<vector<int> > segP[10001], segM[10001];
int get(int i, int s, int e, vector<vector<int> > &seg, int val, bool f) {
  if (s >= l && e <= r) {
    if (!f)
      return seg[i].size() -
             (lower_bound((seg[i]).begin(), (seg[i]).end(), val) -
              seg[i].begin());
    return upper_bound((seg[i]).begin(), (seg[i]).end(), val) - seg[i].begin();
  }
  if (s > r || e < l) return 0;
  return get(i * 2, s, (s + e) / 2, seg, val, f) +
         get(i * 2 + 1, (s + e) / 2 + 1, e, seg, val, f);
}
void build(int i, int s, int e, vector<vector<int> > &seg, vector<int> &v) {
  if (s == e) {
    seg[i].push_back(v[s - 1]);
    return;
  }
  build(i * 2, s, (s + e) / 2, seg, v);
  build(i * 2 + 1, (s + e) / 2 + 1, e, seg, v);
  for (int j = 0; j < seg[i * 2].size(); ++j) seg[i].push_back(seg[i * 2][j]);
  for (int j = 0; j < seg[i * 2 + 1].size(); ++j)
    seg[i].push_back(seg[i * 2 + 1][j]);
  sort((seg[i]).begin(), (seg[i]).end());
}
int main() {
  cin >> n >> k;
  for (int x, rd, f, i = 0; i < n; ++i) {
    scanf("%d%d%d", &x, &rd, &f);
    v[f].push_back(pair<int, int>(x, rd));
  }
  long long ans = 0;
  for (int i = 0; i < 10001; ++i) {
    if (v[i].empty()) continue;
    sort((v[i]).begin(), (v[i]).end());
    P[i].resize(v[i].size());
    M[i].resize(v[i].size());
    segP[i].resize(4 * v[i].size() + 1);
    segM[i].resize(4 * v[i].size() + 1);
    for (int j = 0; j < v[i].size(); ++j) {
      P[i][j] = v[i][j].first + v[i][j].second;
      M[i][j] = v[i][j].first - v[i][j].second;
    }
    build(1, 1, v[i].size(), segP[i], P[i]);
    build(1, 1, v[i].size(), segM[i], M[i]);
    for (int j = 0; j < v[i].size(); ++j) {
      int x = v[i][j].first;
      int p = P[i][j];
      int m = M[i][j];
      for (int t = max(0, i - k); t <= i; ++t) {
        if (v[t].empty()) continue;
        l = lower_bound((v[t]).begin(), (v[t]).end(), pair<int, int>(m, -oo)) -
            v[t].begin() + 1;
        r = upper_bound((v[t]).begin(), (v[t]).end(), pair<int, int>(x, oo)) -
            v[t].begin();
        ans += get(1, 1, v[t].size(), segP[t], x, 0);
        if (t == i) continue;
        l = r + 1;
        r = upper_bound((v[t]).begin(), (v[t]).end(), pair<int, int>(p, oo)) -
            v[t].begin();
        ans += get(1, 1, v[t].size(), segM[t], x, 1);
      }
    }
  }
  cout << ans - n;
}
