#include <bits/stdc++.h>
using namespace std;
const int BUFFER_SIZE = 1 << 20;
char rb[BUFFER_SIZE], *rp = rb, *rt = rb;
inline char read_char() {
  return rp == rt ? (rt = rb + fread(rb, 1, BUFFER_SIZE, stdin), rp = rb, *rp++)
                  : *rp++;
}
inline int read_int() {
  int x = 0;
  char ch = read_char(), flag = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) {
    ch = read_char();
  }
  if (ch == '-') {
    flag = 1;
    ch = read_char();
  }
  for (x = 0; ch >= '0' && ch <= '9'; ch = read_char()) {
    x = x * 10 + (ch - '0');
  }
  return flag ? -x : x;
}
const int MAX_N = 100000 + 5;
int N, K, F, ai[MAX_N], ai_[MAX_N];
long long ans;
struct Node {
  int x, r, f;
} a[MAX_N];
bool cmp_by_r(int i, int j) { return a[i].r > a[j].r; }
bool cmp_by_x(int i, int j) { return a[i].x < a[j].x; }
bool cmp_by_right(int i, int j) { return a[i].x + a[i].r < a[j].x + a[j].r; }
bool cmp_by_left(int i, int j) { return a[i].x - a[i].r < a[j].x - a[j].r; }
struct BIT {
  int c[MAX_N];
  void modify(int f, int v) {
    for (int j = f; j <= F; j += j & -j) {
      c[j] += v;
    }
  }
  int query(int f) {
    int res = 0;
    for (int j = f + K; j >= 1; j -= j & -j) {
      res += c[j];
    }
    for (int j = f - K - 1; j >= 1; j -= j & -j) {
      res -= c[j];
    }
    return res;
  }
} B;
void solve(int L, int R) {
  if (L == R) {
    return;
  }
  int Mid = L + R >> 1;
  solve(L, Mid);
  solve(Mid + 1, R);
  sort(ai + L, ai + Mid + 1, cmp_by_x);
  sort(ai + Mid + 1, ai + R + 1, cmp_by_right);
  for (int i = L, j = Mid + 1; i <= Mid || j <= R;) {
    if (i <= Mid && (j > R || a[ai[i]].x <= a[ai[j]].x + a[ai[j]].r)) {
      B.modify(a[ai[i]].f, 1);
      i++;
    } else {
      ans += B.query(a[ai[j]].f);
      j++;
    }
  }
  sort(ai + Mid + 1, ai + R + 1, cmp_by_left);
  for (int i = Mid, j = R; i >= L || j > Mid;) {
    if (i >= L && (j <= Mid || a[ai[i]].x >= a[ai[j]].x - a[ai[j]].r)) {
      B.modify(a[ai[i]].f, -1);
      i--;
    } else {
      ans -= B.query(a[ai[j]].f);
      j--;
    }
  }
}
int main() {
  N = read_int();
  K = read_int();
  for (int i = 1; i <= N; i++) {
    a[i].x = read_int();
    a[i].r = read_int();
    a[i].f = read_int();
    F = max(F, a[i].f);
    ai[i] = i;
  }
  F += K;
  sort(ai + 1, ai + 1 + N, cmp_by_r);
  solve(1, N);
  printf("%lld\n", ans);
  return 0;
}
