#include <bits/stdc++.h>
using namespace std;
int a, s, d, f, g, h, j, k, l[1002], i, n, res, i1, flag;
string from[1002], to[1002], c;
int check(int i9, string x1, string x2) {
  int ix;
  for (ix = i9 + 1; ix < n; ix++) {
    if ((x2 == from[ix]) && (to[ix] == x1)) {
      to[ix] = "*";
      from[ix] = "*";
    }
    if ((x2 == to[ix]) && (from[ix] == x1)) {
      to[ix] = "*";
      from[ix] = "*";
    }
  }
}
int main() {
  scanf("%d%d\n", &n, &k);
  for (i = 0; i < n; i++) {
    cin >> from[i] >> to[i] >> l[i];
  }
  for (i = 0; i < n; i++) {
    flag = 0;
    if (from[a] == "*") continue;
    for (a = i; a < n; a++) {
      if (from[i] == "*") continue;
      i1 = l[a] - l[i];
      if (i1 < 0) i1 = -i1;
      if ((to[i] == from[a]) && (to[a] == from[i]) && (i1 <= k) &&
          (l[a] != l[i]) && (flag == 0)) {
        res++;
        flag = 1;
        to[a] = "*";
        from[a] = "*";
      }
    }
    if (flag == 0) {
      to[i] = "*";
      from[i] = "*";
    } else
      check(i, from[i], to[i]);
  }
  printf("%d\n", res);
  for (i = 0; i < n; i++) {
    if (from[i] != "*") cout << from[i] << " " << to[i] << endl;
  }
}
