#include <bits/stdc++.h>
using namespace std;
class Message {
 public:
  string from;
  string to;
  int t;
  Message() : from(""), to(""), t(0) {}
  Message(string from, string to, int t) : from(from), to(to), t(t) {}
};
int main(int argc, char** argv) {
  int n, d;
  cin >> n >> d;
  vector<Message> messages(n);
  set<pair<string, string> > friends;
  string a, b;
  int t;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b >> t;
    messages[i] = Message(a, b, t);
  }
  for (int i = 0; i < n - 1; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (messages[i].to == messages[j].from &&
          messages[i].from == messages[j].to) {
        int dt = messages[j].t - messages[i].t;
        if (0 < dt && dt <= d) {
          a = messages[i].to;
          b = messages[i].from;
          if (a > b) {
            swap(a, b);
          }
          friends.insert(make_pair(a, b));
        }
      }
    }
  }
  cout << friends.size() << "\n";
  for_each(friends.begin(), friends.end(), [](const pair<string, string>& f) {
    cout << f.first << " " << f.second << "\n";
  });
}
