#include <bits/stdc++.h>
using namespace std;
int n, d;
char user1[1010][25];
char user2[1010][25];
int t[1010];
bool friends[1010];
int ret;
char ret1[1010][25];
char ret2[1010][25];
bool strcmp(char *s1, char *s2) {
  int len1 = strlen(s1);
  int len2 = strlen(s2);
  if (len1 != len2) return false;
  for (int i = 0; i < len1; ++i)
    if (s1[i] != s2[i]) return false;
  return true;
}
int main() {
  ret = 0;
  scanf("%d%d", &n, &d);
  for (int i = 0; i < n; ++i) {
    scanf("%s%s%d", user1[i], user2[i], &t[i]);
    bool ima = false;
    for (int j = 0; j < ret; ++j) {
      if (strcmp(user1[i], ret1[j]) && strcmp(user2[i], ret2[j])) ima = true;
      if (strcmp(user2[i], ret1[j]) && strcmp(user1[i], ret2[j])) ima = true;
    }
    if (!ima) {
      bool ok = false;
      for (int j = 0; j < i; ++j) {
        if (strcmp(user2[i], user1[j]) && strcmp(user1[i], user2[j]) &&
            t[i] - t[j] <= d && t[i] - t[j] > 0) {
          ok = true;
        }
      }
      if (ok) {
        strcpy(ret1[ret], user1[i]);
        strcpy(ret2[ret], user2[i]);
        ret++;
      }
    }
  }
  printf("%d\n", ret);
  for (int i = 0; i < ret; ++i) {
    printf("%s %s\n", ret1[i], ret2[i]);
  }
  return 0;
}
