#include <bits/stdc++.h>
using namespace std;
class Avl_tree {
 private:
  struct Node {
    Node *l, *r;
    int key, val;
    int height_l, height_r;
    int sz_l, sz_r;
    Node(Node *l, Node *r, int key, int val) {
      this->l = l;
      this->r = r;
      this->key = key;
      this->val = val;
      this->height_l = 0;
      this->height_r = 0;
      this->sz_l = this->sz_r = 0;
    }
    Node() {
      l = r = NULL;
      key = val = 0;
      this->height_l = 0;
      this->height_r = 0;
      this->sz_l = this->sz_r = 0;
    }
  } * root;
  void rotate_left(Node *&node) {
    Node *temp = node->r;
    node->height_r = node->r->height_l;
    node->sz_r = node->r->sz_l;
    node->r = node->r->l;
    temp->height_l = max(node->height_r, node->height_l) + 1;
    temp->sz_l = node->sz_l + node->sz_r + 1;
    temp->l = node;
    node = temp;
  }
  void rotate_right(Node *&node) {
    Node *temp = node->l;
    node->height_l = node->l->height_r;
    node->sz_l = node->l->sz_r;
    node->l = node->l->r;
    temp->height_r = max(node->height_r, node->height_l) + 1;
    temp->sz_r = node->sz_r + node->sz_l + 1;
    temp->r = node;
    node = temp;
  }
  int count(int key, Node *&node) {
    if (node != NULL) {
      if (key < node->key)
        count(key, node->l);
      else if (key > node->key) {
        return node->sz_l + count(key, node->r) + 1;
      } else
        return node->sz_l;
    } else
      return 0;
  }
  void insert(int key, int val, Node *&node) {
    if (node == NULL) {
      node = new Node(NULL, NULL, key, val);
    } else {
      if (key < node->key) {
        insert(key, val, node->l);
        node->height_l = max(node->l->height_l, node->l->height_r) + 1;
        node->sz_l = node->l->sz_l + node->l->sz_r + 1;
      } else {
        insert(key, val, node->r);
        node->height_r = max(node->r->height_l, node->r->height_r) + 1;
        node->sz_r = node->r->sz_l + node->r->sz_r + 1;
      }
      if (node->height_l - node->height_r > 1) {
        if (node->l->height_l - node->l->height_r >= 0) {
          rotate_right(node);
        } else {
          rotate_left(node->l);
          rotate_right(node);
        }
      } else if (node->height_l - node->height_r < -1) {
        if (node->r->height_l - node->r->height_r <= 0) {
          rotate_left(node);
        } else {
          rotate_right(node->r);
          rotate_left(node);
        }
      }
    }
  }
  void remove(int key, Node *&node) {
    if (node) {
      if (key < node->key) {
        remove(key, node->l);
        if (node->l) {
          node->height_l = max(node->l->height_l, node->l->height_r) + 1;
          node->sz_l = node->l->sz_l + node->l->sz_r + 1;
        } else {
          node->height_l = 0;
          node->sz_l = 0;
        }
      } else if (key > node->key) {
        remove(key, node->r);
        if (node->r) {
          node->height_r = max(node->r->height_l, node->r->height_r) + 1;
          node->sz_r = node->r->sz_l + node->r->sz_r + 1;
        } else {
          node->height_r = 0;
          node->sz_r = 0;
        }
      } else if (key == node->key) {
        if (node->l == node->r && node->l == NULL) {
          delete node;
          node = NULL;
        } else if (node->l == NULL && node->r != NULL) {
          Node *p = node;
          node = node->r;
          delete p;
        } else if (node->l != NULL && node->r == NULL) {
          Node *p = node;
          node = node->l;
          delete p;
        } else {
          int key = find_kth_element(node->sz_l + 2, node);
          node->key = key;
          remove(key, node->r);
          if (node->r) {
            node->height_r = max(node->r->height_l, node->r->height_r) + 1;
            node->sz_r = node->r->sz_l + node->r->sz_r + 1;
          } else {
            node->height_r = 0;
            node->sz_r = 0;
          }
        }
      }
      if (node) {
        if (node->height_l - node->height_r > 1) {
          if (node->l->height_l - node->l->height_r >= 0) {
            rotate_right(node);
          } else {
            rotate_left(node->l);
            rotate_right(node);
          }
        } else if (node->height_l - node->height_r < -1) {
          if (node->r->height_l - node->r->height_r <= 0) {
            rotate_left(node);
          } else {
            rotate_right(node->r);
            rotate_left(node);
          }
        }
      }
    }
  }
  bool find(int key, Node *&node) {
    if (node != NULL) {
      if (key < node->key)
        find(key, node->l);
      else if (key > node->key)
        find(key, node->r);
      else
        return true;
    } else
      return false;
  }
  int find_kth_element(int k, Node *&node) {
    if (node != NULL) {
      if (k < node->sz_l + 1)
        find_kth_element(k, node->l);
      else if (k > node->sz_l + 1)
        find_kth_element(k - (node->sz_l + 1), node->r);
      else
        return node->key;
    }
  }

 public:
  Avl_tree() { root = NULL; }
  void insert(int key, int val) { insert(key, val, root); }
  void remove(int key) { remove(key, root); }
  bool find(int key) { return find(key, root); }
  int count(int key) { return count(key, root); }
  int find_kth_element(int k) { return find_kth_element(k, root); }
  int size() {
    if (root)
      return root->sz_l + root->sz_r + 1;
    else
      return 0;
  }
};
struct Elem {
  int p, f, r, ev, i;
  Elem(int x = 0, int y = 0, int z = 0, int t = 0, int c = 0)
      : p(x), f(y), r(t), ev(z), i(c){};
};
bool comparator(Elem &e1, Elem &e2) {
  if (e1.p == e2.p)
    return e1.ev < e2.ev;
  else
    return e1.p < e2.p;
}
Avl_tree h[10010];
vector<Elem> v;
int main() {
  int N, K;
  cin >> N >> K;
  v.push_back(Elem(-2111111111));
  for (int i = 1; i <= N; ++i) {
    int x, r, p;
    cin >> x >> r >> p;
    v.push_back(Elem(x, p, 1, r, i));
    v.push_back(Elem(x + r, p, 2, r, i));
  }
  sort(v.begin(), v.end(), comparator);
  long long rez = 0;
  for (int i = 1; i < v.size(); ++i) {
    if (v[i].ev == 1) {
      for (int j = 1; j <= K; ++j) {
        rez +=
            h[v[i].f + j].count(v[i].p) - h[v[i].f + j].count(v[i].p - v[i].r);
        if (v[i].f - j >= 1)
          rez += h[v[i].f - j].count(v[i].p) -
                 h[v[i].f - j].count(v[i].p - v[i].r);
      }
      rez += h[v[i].f].count(v[i].p) - h[v[i].f].count(v[i].p - v[i].r);
      h[v[i].f].insert(v[i].p, 0);
    } else {
      h[v[i].f].remove(v[i].p - v[i].r);
    }
  }
  cout << rez;
  return 0;
}
