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