#include <bits/stdc++.h>
using namespace std;
int readint() {
  int i;
  scanf("%d", &i);
  return i;
}
map<string, int> names;
map<int, string> rev;
struct item {
  int A, B, t;
};
int get(const string& v) {
  map<string, int>::iterator it = names.find(v);
  if (it == names.end()) {
    int id = names.size();
    names[v] = id;
    rev[id] = v;
    return id;
  }
  return it->second;
}
vector<item> is;
set<pair<int, int> > ps;
int main() {
  int n = readint(), d = readint();
  is.resize(n);
  for (int i = 0; i < int(n); ++i) {
    string A, B;
    cin >> A >> B;
    is[i].A = get(A);
    is[i].B = get(B);
    is[i].t = readint();
  }
  for (int i = 0; i < int(n); ++i)
    for (int j = 0; j < int(n); ++j)
      if (is[i].t < is[j].t && is[i].B == is[j].A && is[j].B == is[i].A &&
          is[j].t - is[i].t <= d) {
        ps.insert(make_pair(is[i].A, is[i].B));
        ps.insert(make_pair(is[i].B, is[i].A));
      }
  printf("%d\n", ps.size() / 2);
  for (set<pair<int, int> >::iterator it = ps.begin(); it != ps.end(); ++it) {
    if (it->first > it->second) continue;
    printf("%s %s\n", rev[it->first].c_str(), rev[it->second].c_str());
  }
  return 0;
}
