#include <bits/stdc++.h>
using namespace std;
int main() {
  set<string> st;
  int n, d;
  string a[1001], b[1001];
  int c[1001];
  cin >> n >> d;
  set<string> sl;
  for (int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      string s1, s2;
      s1 = a[i];
      s2 = b[i];
      if (s1 > s2) swap(s1, s2);
      if (a[i] == b[j] && a[j] == b[i] && c[j] - c[i] > 0 && c[j] - c[i] <= d) {
        if (sl.find(s1 + " " + s2) == sl.end()) {
          sl.insert(s1 + " " + s2);
        }
      }
    }
  cout << sl.size() << '\n';
  for (set<string>::iterator it = sl.begin(); it != sl.end(); it++)
    cout << *it << '\n';
  return 0;
}
