#include <bits/stdc++.h>
using namespace std;
const int limit = 200, N = 1e5 + 5, M = 1e4 + 15;
struct Node {
  int x, r, f;
  Node() {}
  Node(int a, int b, int c) {
    x = a;
    r = b;
    f = c;
  }
} a[N];
int n, m, cnt[M], label;
long long ans;
set<pair<int, int> > q;
map<int, int> Q;
int c[100][N];
vector<pair<int, int> > C[M];
int cmp(Node a, Node b) { return a.x < b.x; }
int ins(int k, int i, int d) {
  if (cnt[k] < limit) {
    if (d == 1) {
      C[k].push_back(make_pair(i, 1));
      return 0;
    }
    for (int j = 0; j < C[k].size(); j++)
      if (C[k][j].first == i) C[k][j].second = 0;
    return 0;
  }
  k = Q[k];
  for (; i <= n; i += i & (-i)) c[k][i] += d;
}
int get(int k, int i) {
  int ret = 0;
  if (cnt[k] < limit) {
    for (int j = 0; j < C[k].size(); j++) {
      if (C[k][j].first > i) break;
      ret += C[k][j].second;
    }
    return ret;
  }
  k = Q[k];
  for (; i > 0; i -= i & (-i)) ret += c[k][i];
  return ret;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d", &a[i].x, &a[i].r, &a[i].f), cnt[a[i].f]++;
    if (Q.find(a[i].f) == Q.end()) Q[a[i].f] = label++;
  }
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    int j = lower_bound(a + 1, a + n + 1, Node(a[i].x - a[i].r, 0, 0), cmp) - a;
    while (!q.empty() && q.begin()->first < a[i].x) {
      int p = q.begin()->second;
      ins(a[p].f, p, -1);
      q.erase(q.begin());
    }
    ins(a[i].f, i, 1);
    q.insert(make_pair(a[i].x + a[i].r, i));
    for (int k = max(1, a[i].f - m); k <= a[i].f + m; k++)
      ans += get(k, i - 1) - get(k, j - 1);
  }
  cout << ans;
}
