#include <bits/stdc++.h>
using namespace std;
bool srh(char *what, char *where, int n);
int main() {
  int n, d, t[1000], z = 0;
  char a[1000][21], b[1000][21], p[2000][21];
  bool dup;
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> t[i];
    for (int j = i - 1; j >= 0; j--) {
      if (!strcmp(a[i], b[j]) && !strcmp(b[i], a[j]) && (t[i] - t[j]) > 0) {
        dup = false;
        for (int k = 0; k < z; k += 2) {
          if (!(strcmp(p[k], a[i]) || strcmp(p[k + 1], b[i])) ||
              !(strcmp(p[k], b[i]) || strcmp(p[k + 1], a[i]))) {
            dup = true;
            ;
            break;
          }
        }
        if (!dup && (t[i] - t[j]) <= d) {
          strcpy(p[z++], a[i]);
          strcpy(p[z++], b[i]);
        }
        break;
      }
    }
  }
  cout << z / 2 << endl;
  for (int i = 0; i < z; i += 2) {
    cout << p[i] << ' ' << p[i + 1] << endl;
  }
  return 0;
}
