#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
vector<int> v;
pair<int, pair<int, int> > arr[N];
vector<int> seg[N << 2];
vector<int> BIT[N << 2];
int MX, n;
void increase(int id, int i, int delta = 1) {
  for (; i < BIT[id].size(); i |= i + 1) BIT[id][i] += delta;
}
int sum(int id, int i) {
  int ans = 0;
  i = min(i, (int)BIT[id].size() - 1);
  for (; i >= 0; i = (i & (i + 1)) - 1) ans += BIT[id][i];
  return ans;
}
int getsum(int id, int L, int R) { return sum(id, R) - sum(id, L - 1); }
void add(int val, int idx, int id = 1, int l = 0, int first = MX) {
  seg[id].push_back(val);
  if (first - l < 2) return;
  int mid = (l + first) / 2;
  if (idx < mid)
    add(val, idx, id * 2, l, mid);
  else
    add(val, idx, id * 2 + 1, mid, first);
}
void inc(int val, int idx, int id = 1, int l = 0, int first = MX) {
  int index =
      lower_bound(seg[id].begin(), seg[id].end(), val) - seg[id].begin();
  increase(id, index);
  if (first - l < 2) return;
  int mid = (l + first) / 2;
  if (idx < mid)
    inc(val, idx, id * 2, l, mid);
  else
    inc(val, idx, id * 2 + 1, mid, first);
}
void build(int id = 1, int l = 0, int first = MX) {
  if (!seg[id].size()) return;
  sort(seg[id].begin(), seg[id].end());
  seg[id].resize(unique(seg[id].begin(), seg[id].end()) - seg[id].begin());
  BIT[id].resize(seg[id].size() + 1);
  if (first - l < 2) return;
  int mid = (l + first) / 2;
  build(id * 2, l, mid);
  build(id * 2 + 1, mid, first);
}
int query(int qs, int qe, int from, int to, int id = 1, int l = 0,
          int first = MX) {
  if (qs >= first or l >= qe) return 0;
  if (BIT[id].size() == 0) return 0;
  if (qs <= l && first <= qe) {
    int idxfrom =
        lower_bound(seg[id].begin(), seg[id].end(), from) - seg[id].begin();
    int idxto =
        upper_bound(seg[id].begin(), seg[id].end(), to) - seg[id].begin() - 1;
    return getsum(id, idxfrom, idxto);
  }
  int mid = (l + first) / 2;
  return query(qs, qe, from, to, id * 2, l, mid) +
         query(qs, qe, from, to, id * 2 + 1, mid, first);
}
int F(int val) { return lower_bound(v.begin(), v.end(), val) - v.begin(); }
int main() {
  int k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &arr[i].second.first, &arr[i].first, &arr[i].second.second);
    v.push_back(arr[i].second.first);
    v.push_back(arr[i].second.first - arr[i].first);
    v.push_back(arr[i].second.first + arr[i].first);
  }
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  MX = v.size();
  sort(arr, arr + n);
  reverse(arr, arr + n);
  for (int i = 0; i < n; i++) {
    add(arr[i].second.second, F(arr[i].second.first));
  }
  build();
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += query(F(arr[i].second.first - arr[i].first),
                 F(arr[i].second.first + arr[i].first) + 1,
                 arr[i].second.second - k, arr[i].second.second + k);
    inc(arr[i].second.second, F(arr[i].second.first));
  }
  printf("%lld\n", ans);
  return 0;
}
