#include <bits/stdc++.h>
using namespace std;
struct user {
  int time;
  string A, B;
  bool friend operator<(const user &a, const user &b) {
    return a.time < b.time;
  }
} seq[1111], tp;
int n, d, k;
set<user> Set;
set<pair<string, string> > Ans;
int main() {
  cin >> n >> d;
  for (int i = 1; i <= n; i++) {
    cin >> seq[i].A >> seq[i].B >> seq[i].time;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++)
      if (seq[j].B == seq[i].A && seq[j].A == seq[i].B) {
        k = seq[j].time - seq[i].time;
        if (k > 0 && k <= d) {
          if (Ans.find(make_pair(seq[i].B, seq[i].A)) != Ans.end()) continue;
          Ans.insert(make_pair(seq[i].A, seq[i].B));
        }
      }
  }
  printf("%d\n", Ans.size());
  for (set<pair<string, string> >::iterator it = Ans.begin(); it != Ans.end();
       ++it)
    printf("%s %s \n", it->first.c_str(), it->second.c_str());
  return 0;
}
