#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d;
  cin >> n >> d;
  map<pair<string, string>, set<int>> messages;
  set<pair<string, string>> friends;
  string A, B;
  int t;
  pair<string, string> pairAB;
  pair<string, string> pairBA;
  set<int> previous_messages;
  for (int i = 0; i < n; i++) {
    cin >> A >> B >> t;
    pairAB = make_pair(A, B);
    pairBA = make_pair(B, A);
    messages[pairAB].insert(t);
    if (messages.count(pairBA) && !friends.count(pairAB) &&
        !friends.count(pairBA)) {
      previous_messages = messages[pairBA];
      for (set<int>::iterator iter = previous_messages.lower_bound(t - d);
           iter != previous_messages.upper_bound(t - 1); ++iter) {
        if (A > B) swap(A, B);
        friends.insert(make_pair(A, B));
        break;
      }
    }
  }
  cout << friends.size() << endl;
  for (set<pair<string, string>>::iterator iter = friends.begin();
       iter != friends.end(); ++iter)
    cout << (*iter).first << " " << (*iter).second << endl;
  return 0;
}
