#include <bits/stdc++.h>
using namespace std;
map<pair<string, string>, pair<int, int> > mp;
set<pair<string, string> > st;
int main() {
  int n, d;
  cin >> n >> d;
  for (int i = 0; i < n; ++i) {
    string s, q;
    int x;
    cin >> s >> q;
    scanf("%d", &x);
    if (mp.count({q, s})) {
      pair<int, int> a = mp[{q, s}];
      int y = x - a.first;
      if (y <= d && y > 0) st.insert({min(s, q), max(s, q)});
      y = x - a.second;
      if (y <= d && y > 0) st.insert({min(s, q), max(s, q)});
    }
    if (!mp.count({s, q}))
      mp[{s, q}] = {x, x};
    else {
      pair<int, int> a = mp[{s, q}];
      if (x != a.first) a.second = a.first, a.first = x;
      mp[{s, q}] = a;
    }
  }
  cout << st.size() << endl;
  for (set<pair<string, string> >::iterator it = st.begin(); it != st.end();
       it++)
    printf("%s %s\n", it->first.c_str(), it->second.c_str());
  return 0;
}
