#include <bits/stdc++.h>
using namespace std;
struct rad {
  long long x, brange, frange;
};
vector<rad> radio[10009];
vector<long long> num;
unordered_map<long long, long long> rankOf, idOf[100009];
bool operator<(const rad &a, const rad &b) { return a.x < b.x; }
struct node {
  node *left, *right;
  int val;
  node(int a = 0, node *b = NULL, node *c = NULL) : val(a), left(b), right(c) {}
  void build(int l, int r) {
    if (l == r) return;
    left = new node();
    right = new node();
    int mid = l + r >> 1;
    left->build(l, mid);
    right->build(mid + 1, r);
  }
  node *update(int l, int r, int idx) {
    if (r < idx || l > idx) return this;
    if (l == r) {
      node *ret = new node(val, left, right);
      ret->val++;
      return ret;
    }
    int mid = l + r >> 1;
    node *ret = new node(val);
    ret->left = left->update(l, mid, idx);
    ret->right = right->update(mid + 1, r, idx);
    ret->val = ret->left->val + ret->right->val;
    return ret;
  }
  int query(int l, int r, int i, int j) {
    if (r < i || l > j) return 0;
    if (i <= l && r <= j) {
      return val;
    }
    int mid = l + r >> 1;
    return left->query(l, mid, i, j) + right->query(mid + 1, r, i, j);
  }
} * root[100009];
int main() {
  long long n, k;
  cin >> n >> k;
  for (long long i = 1; i <= n; i++) {
    long long x, r, f;
    scanf("%lld %lld %lld", &x, &r, &f);
    radio[f].push_back({x, max(0LL, x - r), x + r});
    num.push_back(x);
    num.push_back(max(0LL, x - r));
    num.push_back(x + r);
  }
  sort(num.begin(), num.end());
  num.erase(unique(num.begin(), num.end()), num.end());
  for (long long i = 0; i < num.size(); i++) {
    rankOf[num[i]] = i;
  }
  for (long long i = 1; i <= 10000; i++) {
    for (long long j = 0; j < radio[i].size(); j++) {
      rad u = radio[i][j];
      u.x = rankOf[u.x], u.brange = rankOf[u.brange];
      u.frange = rankOf[u.frange];
      radio[i][j] = u;
    }
    sort(radio[i].begin(), radio[i].end());
  }
  long long tot = 0, id = 0;
  root[0] = new node();
  root[0]->build(0, num.size());
  for (long long i = 1; i <= 10000; i++) {
    for (long long j = 0; j < radio[i].size(); j++) {
      id++;
      if (j == 0)
        root[id] = root[0]->update(0, num.size(), radio[i][j].brange);
      else
        root[id] = root[id - 1]->update(0, num.size(), radio[i][j].brange);
      idOf[i][j] = id;
    }
  }
  for (long long i = 1; i <= 10000; i++) {
    long long start = max(1LL, i - k), stop = min((long long)10000, i + k);
    for (long long j = 0; j < radio[i].size(); j++) {
      for (long long m = start; m <= stop; m++) {
        if (radio[m].size() == 0) continue;
        rad key = radio[i][j];
        long long pos1 = lower_bound(radio[m].begin(), radio[m].end(), key) -
                         radio[m].begin();
        swap(key.x, key.frange);
        long long pos2 = lower_bound(radio[m].begin(), radio[m].end(), key) -
                         radio[m].begin();
        if (pos2 == radio[m].size() || radio[m][pos2].x > radio[i][j].frange)
          pos2--;
        if (pos1 > pos2) continue;
        long long val1 =
            root[idOf[m][pos2]]->query(0, num.size(), 0, radio[i][j].x);
        long long val2 = 0;
        if (pos1 != 0)
          val2 =
              root[idOf[m][pos1 - 1]]->query(0, num.size(), 0, radio[i][j].x);
        tot += (val1 - val2);
      }
      tot--;
    }
  }
  cout << tot << endl;
  return 0;
}
