#include <bits/stdc++.h>
using namespace std;
struct ektamessage {
  string sender, receiver;
  int time;
};
int main() {
  int i, j, n, d;
  vector<ektamessage> v;
  vector<pair<string, string> > ans;
  map<pair<string, string>, vector<int> > pathaise;
  map<pair<string, string>, int> nisi;
  ektamessage x;
  cin >> n >> d;
  for (i = 0; i < n; i++) {
    cin >> x.sender >> x.receiver >> x.time;
    v.push_back(x);
    pathaise[make_pair(x.sender, x.receiver)].push_back(x.time);
  }
  for (i = 0; i < n - 1; i++) {
    for (j = i + 1; j < n; j++) {
      if (v[i].sender == v[j].receiver && v[i].receiver == v[j].sender &&
          v[i].time != v[j].time && abs(v[i].time - v[j].time) <= d &&
          !nisi[make_pair(v[i].sender, v[j].sender)] &&
          !nisi[make_pair(v[i].sender, v[j].sender)]) {
        ans.push_back(make_pair(v[i].sender, v[j].sender));
        nisi[make_pair(v[i].sender, v[j].sender)] = 1;
        nisi[make_pair(v[j].sender, v[i].sender)] = 1;
      }
    }
  }
  int len = ans.size();
  cout << len << "\n";
  for (i = 0; i < len; i++) {
    cout << ans[i].first << " " << ans[i].second << "\n";
  }
  return 0;
}
