#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const int MX = 1000000000;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline long long lin() {
  long long x;
  scanf("%I64d", &x);
  return x;
}
struct Info {
  int x, r, f;
  Info() {}
  Info(int _x, int _r, int _f) {
    x = _x;
    r = _r;
    f = _f;
  }
  bool operator<(const Info &other) const { return r > other.r; }
} inp[N];
struct Node {
  int cnt;
  Node *left, *right;
  Node() {
    left = right = NULL;
    cnt = 0;
  }
  Node(int _cnt, Node *_left, Node *_right) {
    cnt = _cnt;
    left = _left;
    right = _right;
  }
} * tree[N];
void modify(Node *x, int b, int e, int idx) {
  if (b > idx || e < idx) {
    return;
  }
  if (b == idx && e == idx) {
    x->cnt++;
    x->left = x->right = NULL;
    return;
  }
  int m = (b + e) / 2;
  if (x->left == NULL) x->left = new Node();
  if (x->right == NULL) x->right = new Node();
  modify(x->left, b, m, idx);
  modify(x->right, m + 1, e, idx);
  x->cnt = x->left->cnt + x->right->cnt;
}
int get(Node *x, int b, int e, int l, int r) {
  if (b > r || e < l || x == NULL) return 0;
  if (b >= l && e <= r) {
    return x->cnt;
  }
  int m = (b + e) >> 1;
  return get(x->left, b, m, l, r) + get(x->right, m + 1, e, l, r);
}
int main() {
  int n = in(), k = in();
  for (int i = 1; i <= n; i++) {
    int x = in(), r = in(), f = in();
    inp[i] = Info(x, r, f);
  }
  sort(inp + 1, inp + n + 1);
  for (int i = 1; i < N; i++) {
    tree[i] = new Node();
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = max(1, inp[i].f - k); j <= min(N, inp[i].f + k); j++) {
      ans += get(tree[j], 1, MX, max(1, inp[i].x - inp[i].r),
                 min(MX, inp[i].x + inp[i].r));
    }
    modify(tree[inp[i].f], 1, MX, inp[i].x);
  }
  printf("%I64d\n", ans);
}
