#include <bits/stdc++.h>
using namespace std;
const int INF = ~(1 << 31);
const double EPS = 1e-8;
int main() {
  int n, d;
  cin >> n >> d;
  vector<string> a(n), b(n);
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  set<pair<string, string> > s;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] == b[j] && a[j] == b[i] && abs(c[i] - c[j]) <= d &&
          abs(c[i] - c[j]) > 0) {
        s.insert(make_pair(min(a[i], b[i]), max(a[i], b[i])));
      }
    }
  }
  printf("%d\n", s.size());
  if (s.size()) {
    for (set<pair<string, string> >::iterator it = s.begin(); it != s.end();
         it++) {
      cout << (*it).first << " " << (*it).second << endl;
    }
  }
  return 0;
}
