#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d;
  cin >> n >> d;
  map<pair<string, string>, set<int> > m;
  for (int i = 0; i < n; i++) {
    string a, b;
    int t;
    cin >> a >> b >> t;
    m[make_pair(a, b)].insert(t);
  }
  map<pair<string, string>, set<int> >::iterator it;
  set<pair<string, string> > s;
  for (it = m.begin(); it != m.end(); it++) {
    string a = (it->first).first;
    string b = (it->first).second;
    if (!s.count(make_pair(a, b)) && !s.count(make_pair(b, a))) {
      set<int>::iterator jt;
      for (jt = (it->second).begin(); jt != (it->second).end(); jt++) {
        set<int>::iterator kt = (m[make_pair(b, a)]).upper_bound((*jt));
        if (kt != (m[make_pair(b, a)]).end() && (*kt) <= (*jt) + d) {
          s.insert(make_pair(a, b));
          break;
        }
      }
    }
  }
  cout << s.size() << "\n";
  set<pair<string, string> >::iterator kt;
  for (kt = s.begin(); kt != s.end(); kt++) {
    cout << kt->first << " " << kt->second << "\n";
  }
  return 0;
}
