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