#include <bits/stdc++.h>
typedef struct Friend {
  char a[25];
  char b[25];
  int p;
} Friend;
typedef struct beFriend {
  char a[25];
  char b[25];
} beFriend;
using namespace std;
int main() {
  int i, n, d, j, q, num = 0;
  Friend friends[1010];
  beFriend befriend[5000];
  cin >> n >> d;
  for (i = 0; i < n; i++) cin >> friends[i].a >> friends[i].b >> friends[i].p;
  for (i = 0; i < n - 1; i++)
    for (j = i + 1; j < n; j++)
      if (strcmp(friends[i].a, friends[j].b) == 0)
        if (strcmp(friends[i].b, friends[j].a) == 0)
          if (friends[j].p - friends[i].p <= d &&
              friends[j].p - friends[i].p > 0) {
            for (q = 0; q < num; q++) {
              if ((strcmp(friends[i].a, befriend[q].a) == 0 &&
                   strcmp(friends[i].b, befriend[q].b) == 0) ||
                  (strcmp(friends[i].b, befriend[q].a) == 0 &&
                   strcmp(friends[i].a, befriend[q].b) == 0))
                break;
            }
            if (q == num) {
              strcpy(befriend[num].a, friends[i].a);
              strcpy(befriend[num].b, friends[i].b);
              num++;
            }
          }
  cout << num << endl;
  for (i = 0; i < num; i++)
    cout << befriend[i].a << " " << befriend[i].b << endl;
  return 0;
}
