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