#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d;
  cin >> n >> d;
  set<pair<string, string> > friends;
  list<pair<pair<string, string>, int> > msgs;
  for (int i = 0; i < n; i++) {
    string name1, name2;
    int t;
    cin >> name1 >> name2 >> t;
    for (list<pair<pair<string, string>, int> >::reverse_iterator i =
             msgs.rbegin();
         i != msgs.rend(); ++i) {
      if (t == i->second) continue;
      if (t - i->second > d) break;
      if (name1.compare(i->first.second) == 0 &&
          name2.compare(i->first.first) == 0) {
        if (name1.compare(name2) < 0)
          friends.insert(make_pair(name1, name2));
        else
          friends.insert(make_pair(name2, name1));
        break;
      }
    }
    msgs.push_back(make_pair(make_pair(name1, name2), t));
  }
  cout << friends.size() << endl;
  for (set<pair<string, string> >::iterator i = friends.begin();
       i != friends.end(); ++i) {
    cout << i->first << " " << i->second << endl;
  }
  return 0;
}
