#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d;
  cin >> n >> d;
  string First[n];
  string Second[n];
  int Time[n];
  for (int i = 0; i < n; ++i) {
    cin >> First[i] >> Second[i] >> Time[i];
  }
  set<pair<string, string> > Friends;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (First[i] == Second[j] && Second[i] == First[j] &&
          Time[j] - Time[i] <= d && Time[j] - Time[i] > 0) {
        Friends.insert(
            make_pair(max(First[i], Second[i]), min(First[i], Second[i])));
      }
    }
  }
  set<pair<string, string> >::iterator it;
  cout << Friends.size() << endl;
  for (it = Friends.begin(); it != Friends.end(); ++it) {
    cout << it->first << ' ' << it->second << endl;
  }
  return 0;
}
