#include <bits/stdc++.h>
using namespace std;
class Message {
 public:
  string sender;
  string receiver;
  int time;
};
struct classcomp {
  bool operator()(const Message& lhs, const Message& rhs) const {
    if (lhs.sender == rhs.sender)
      return lhs.receiver < rhs.receiver;
    else
      return lhs.sender < rhs.sender;
  }
};
int main() {
  int n, d, time;
  vector<Message> messages;
  set<Message, classcomp> result;
  cin >> n >> d;
  while (n--) {
    Message m;
    cin >> m.sender >> m.receiver >> m.time;
    messages.push_back(m);
  }
  for (int i = 0; i < messages.size(); ++i)
    for (int j = 0; j < messages.size(); ++j) {
      if (j == i) continue;
      if (messages[j].sender == messages[i].receiver &&
          messages[j].receiver == messages[i].sender &&
          abs(messages[j].time - messages[i].time) > 0 &&
          abs(messages[j].time - messages[i].time) <= d) {
        Message m;
        if (messages[i].sender.compare(messages[i].receiver) > 0) {
          m.sender = messages[i].sender;
          m.receiver = messages[i].receiver;
        } else {
          m.sender = messages[i].receiver;
          m.receiver = messages[i].sender;
        }
        m.time = 0;
        result.insert(m);
      }
    }
  cout << result.size() << endl;
  for (typeof((result).begin()) it = (result).begin(); it != (result).end();
       it++)
    cout << it->sender << " " << it->receiver << endl;
}
