#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d, last = 0;
  scanf("%d%d", &n, &d);
  vector<string> a(n), b(n);
  vector<int> t(n);
  set<pair<string, string> > fr;
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i] >> t[i];
    for (int j = i - 1; j >= 0; --j)
      if ((a[i] == b[j]) && (b[i] == a[j]) && (t[i] != t[j]) &&
          (t[i] - t[j] <= d)) {
        if (a[i] > b[i]) swap(a[i], b[i]);
        fr.insert(make_pair(a[i], b[i]));
      }
  }
  cout << fr.size() << endl;
  for (set<pair<string, string> >::iterator i = fr.begin(); i != fr.end(); ++i)
    printf("%s %s\n", i->first.c_str(), i->second.c_str());
  cout << "";
  return 0;
}
