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