#include <bits/stdc++.h>
using namespace std;
char A[21], B[21];
long i, N, d, t, j, lg;
map<string, vector<pair<string, long> > > lista;
map<string, vector<pair<string, long> > >::const_iterator iter;
vector<pair<string, string> > rez;
string a, b;
inline int modul(long x) { return (x < 0) ? -x : ((x == 0) ? d + 1 : x); }
void adauga(string a, string b) {
  bool ok = true;
  long q, lg;
  for (q = 0, lg = rez.size(); q < lg; ++q) {
    if (rez[q].first.compare(a) == 0 && rez[q].second.compare(b) == 0) {
      ok = false;
      break;
    }
  }
  if (ok) {
    rez.push_back(make_pair(a, b));
  }
}
int main() {
  scanf("%ld %ld", &N, &d);
  for (i = 1; i <= N; ++i) {
    scanf("%s %s %ld", A, B, &t);
    a = A;
    b = B;
    for (j = 0, lg = lista[b].size(); j < lg; ++j) {
      if (lista[b][j].first == a) {
        if (modul(lista[b][j].second - t) <= d) {
          if (a.compare(b) < 0) {
            adauga(a, b);
          } else {
            adauga(b, a);
          }
        }
      }
    }
    lista[a].push_back(make_pair(b, t));
  }
  printf("%ld\n", lg = rez.size());
  for (i = 0; i < lg; ++i) {
    printf("%s %s\n", rez[i].first.c_str(), rez[i].second.c_str());
  }
  return 0;
}
