#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const int maxn = 3e5 + 10;
int n, k, siz;
int ans[maxn];
vector<int> vx, viq;
int bit[maxn];
int lowbit(int x) { return x & -x; }
void update(int iq, int flag) {
  for (int i = iq; i < maxn; i += lowbit(i)) bit[i] += flag;
}
int query(int iq) {
  int ans = 0;
  for (int i = iq; i > 0; i -= lowbit(i)) ans += bit[i];
  return ans;
}
struct Node {
  int iq, pos, flag, low, high, id, r;
  Node() { iq = pos = flag = low = high = id = r = 0; }
  Node(int iq, int pos, int flag, int low, int high, int id, int r) {
    this->iq = iq;
    this->pos = pos;
    this->flag = flag;
    this->low = low;
    this->high = high;
    this->id = id;
    this->r = r;
  }
  bool operator<(const Node &b) const {
    if (pos == b.pos) return flag < b.flag;
    return pos < b.pos;
  }
} b[maxn], tmp[maxn];
int cmpinit(const Node &a, const Node &b) {
  if (a.r == b.r) return a.flag < b.flag;
  return a.r > b.r;
}
void cdq(int l, int r) {
  if (l == r) return;
  cdq(l, (l + r) / 2);
  cdq((l + r) / 2 + 1, r);
  sort(b + l, b + (l + r) / 2 + 1);
  sort(b + (l + r) / 2 + 1, b + r + 1);
  int p = l, q = (l + r) / 2 + 1;
  while (p <= (l + r) / 2 && q <= r) {
    if (b[p] < b[q]) {
      if (!b[p].flag) update(b[p].iq, 1);
      p++;
    } else {
      if (b[q].flag) {
        int sum1 = query(b[q].low), sum2 = query(b[q].high);
        ans[b[q].id] += (sum2 - sum1) * (b[q].flag == 1 ? -1 : 1);
      }
      q++;
    }
  }
  while (p <= (l + r) / 2) {
    if (!b[p].flag) update(b[p].iq, 1);
    p++;
  }
  while (q <= r) {
    if (b[q].flag) {
      int sum1 = query(b[q].low), sum2 = query(b[q].high);
      ans[b[q].id] += (sum2 - sum1) * (b[q].flag == 1 ? -1 : 1);
    }
    q++;
  }
  p = l;
  while (p <= (l + r) / 2) {
    if (!b[p].flag) update(b[p].iq, -1);
    p++;
  }
}
int checkiq(int x) {
  int l = 0, r = viq.size() - 1;
  while (1) {
    if (l + 1 >= r) {
      if (x == viq[l]) return l + 1;
      return r + 1;
    }
    if (viq[(l + r) / 2] < x)
      l = (l + r) / 2 + 1;
    else
      r = (l + r) / 2;
  }
}
int checkx(int x) {
  int l = 0, r = vx.size() - 1;
  while (1) {
    if (l + 1 >= r) {
      if (x == vx[l]) return l + 1;
      return r + 1;
    }
    if (vx[(l + r) / 2] < x)
      l = (l + r) / 2 + 1;
    else
      r = (l + r) / 2;
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    int x, r, iq;
    scanf("%d%d%d", &x, &r, &iq);
    tmp[i].pos = x;
    tmp[i].low = x - r - 1;
    tmp[i].iq = iq;
    tmp[i].r = r;
    vx.push_back(x);
    vx.push_back(x - r - 1);
    vx.push_back(x + r);
    viq.push_back(iq);
    viq.push_back(iq - k - 1);
    viq.push_back(iq + k);
  }
  sort(vx.begin(), vx.end());
  sort(viq.begin(), viq.end());
  vx.erase(unique(vx.begin(), vx.end()), vx.end());
  viq.erase(unique(viq.begin(), viq.end()), viq.end());
  sort(tmp + 1, tmp + n + 1, cmpinit);
  for (int i = 1; i <= n; i++) {
    b[++siz] =
        Node(checkiq(tmp[i].iq), checkx(tmp[i].pos), 0, 0, 0, i, tmp[i].r);
    b[++siz] = Node(0, checkx(tmp[i].low), 1, checkiq(tmp[i].iq - k - 1),
                    checkiq(tmp[i].iq + k), i, tmp[i].r);
    b[++siz] =
        Node(0, checkx(tmp[i].pos + tmp[i].r), 2, checkiq(tmp[i].iq - k - 1),
             checkiq(tmp[i].iq + k), i, tmp[i].r);
  }
  cdq(1, siz);
  long long sum = 0;
  for (int i = 1; i <= n; i++) sum += 1LL * (ans[i] - 1);
  printf("%lld\n", sum);
  return 0;
}
