#include <bits/stdc++.h>
using namespace std;
int n, k, m;
long long ans;
int pos1[100005], pos2[100005];
struct node {
  int x, y, z, L, R;
  int id, flag;
} p[100005];
bool cmp1(node a, node b) {
  if (a.y != b.y) return a.y > b.y;
  if (a.z != b.z) return a.z < b.z;
  return a.x < b.x;
}
bool cmp2(node a, node b) {
  if (a.z != b.z) return a.z < b.z;
  if (a.x != b.x) return a.x < b.x;
  return a.y > b.y;
}
int c[3 * 100005];
void add(int x, int v) {
  for (; x <= m; x += x & -x) c[x] += v;
}
int ask(int x) {
  int sum = 0;
  for (; x; x -= x & -x) sum += c[x];
  return sum;
}
void clear(int x) {
  for (; x <= m; x += x & -x) {
    if (!c[x]) return;
    c[x] = 0;
  }
}
void solve(int l, int r) {
  if (l == r) return;
  int mid = (l + r) >> 1;
  solve(l, mid), solve(mid + 1, r);
  sort(p + l, p + mid + 1, cmp2);
  sort(p + mid + 1, p + r + 1, cmp2);
  int j1 = l, j2 = l - 1;
  for (int i = mid + 1; i <= r; i++) {
    while (j1 <= mid && p[i].z - p[j1].z > k) add(p[j1].x, -1), j1++;
    while (j2 < mid && p[j2 + 1].z - p[i].z <= k) {
      j2++;
      add(p[j2].x, 1);
    }
    ans += ask(p[i].R) - ask(p[i].L - 1);
  }
  for (int i = j1; i <= j2; i++) add(p[i].x, -1);
}
int b[3 * 100005], cnt;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    int a, r, c;
    scanf("%d%d%d", &a, &r, &c);
    p[i].x = a;
    p[i].y = r;
    p[i].z = c;
    p[i].L = max(0, a - r);
    p[i].R = a + r;
    b[++cnt] = p[i].x;
    b[++cnt] = p[i].L;
    b[++cnt] = p[i].R;
  }
  sort(b + 1, b + cnt + 1);
  m = unique(b + 1, b + cnt + 1) - b - 1;
  for (int i = 1; i <= n; i++) {
    p[i].x = lower_bound(b + 1, b + m + 1, p[i].x) - b;
    p[i].L = lower_bound(b + 1, b + m + 1, p[i].L) - b;
    p[i].R = lower_bound(b + 1, b + m + 1, p[i].R) - b;
  }
  sort(p + 1, p + n + 1, cmp1);
  solve(1, n);
  printf("%lld\n", ans);
  return 0;
}
