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