#include <bits/stdc++.h>
using namespace std;
struct node {
  string s1;
  string s2;
  int t;
} a[1004], b[1004 / 2];
int main() {
  int n, d;
  cin >> n >> d;
  for (int i = 0; i < n; i++) cin >> a[i].s1 >> a[i].s2 >> a[i].t;
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      if (abs(a[i].t - a[j].t) <= d && a[i].t != a[j].t && a[i].s1 == a[j].s2 &&
          a[i].s2 == a[j].s1) {
        int flag = 0;
        for (int k = 0; k <= ans; k++)
          if ((a[i].s1 == b[k].s1 && a[i].s2 == b[k].s2) ||
              (a[i].s1 == b[k].s2 && a[i].s2 == b[k].s1)) {
            flag = 1;
            break;
          }
        if (flag == 0) {
          ans++;
          b[ans].s1 = a[i].s1;
          b[ans].s2 = a[i].s2;
        }
      }
  cout << ans << endl;
  if (ans)
    for (int i = 1; i <= ans; i++) cout << b[i].s1 << " " << b[i].s2 << endl;
  return 0;
}
