#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, d, j = 1, k, l;
  bool p = true;
  cin >> n >> d;
  char **c = new char *[n + 1];
  char **e = new char *[n + 1];
  char **c_c = new char *[n + 1];
  char **e_e = new char *[n + 1];
  for (i = 0; i < n; ++i) {
    c[i] = new char[21];
    e[i] = new char[21];
    c_c[i] = new char[21];
    e_e[i] = new char[21];
  }
  int *t = new int[n + 1];
  cin >> c[0] >> e[0] >> t[0];
  for (i = 1; i < n; ++i) {
    cin >> c[i] >> e[i] >> t[i];
    for (l = 0; l < i; ++l) {
      if (((!strcmp(c[i], e[l])) && (!strcmp(e[i], c[l]))) &&
          ((t[i] - t[l] <= d) && (t[i] - t[l] > 0))) {
        for (k = 0; k < j; ++k) {
          if (((!strcmp(c[i], c_c[k])) && (!strcmp(e[i], e_e[k]))) ||
              ((!strcmp(c[i], e_e[k])) && (!strcmp(e[i], c_c[k])))) {
            p = false;
          }
        }
        if (p) {
          c_c[j - 1] = c[i];
          e_e[j - 1] = e[i];
          j++;
        }
      }
      p = true;
    }
  }
  cout << j - 1 << endl;
  for (i = 0; i < j - 1; ++i) cout << c_c[i] << " " << e_e[i] << endl;
  return 0;
}
