#include <bits/stdc++.h>
using namespace std;
string A[1002], B[1002];
int t[1002];
int nom[1002];
int main() {
  int i, j, n, d, ndr = 0, tpr;
  cin >> n >> d;
  for (i = 0; i < n; i++) cin >> A[i] >> B[i] >> t[i];
  for (i = 0; i < n; i++) {
    tpr = t[i];
    for (j = i + 1; j < n; j++)
      if ((t[j] - tpr) <= d && (t[j] - tpr) > 0)
        if ((A[j] == B[i]) && (B[j] == A[i])) {
          nom[i] = 1;
          break;
        }
  }
  for (i = 0; i < n; i++)
    if (nom[i]) {
      for (j = 0; j < i; j++)
        if (nom[j])
          if (((A[j] == B[i]) && (B[j] == A[i])) ||
              ((A[j] == A[i]) && (B[j] == B[i]))) {
            nom[i] = 0;
            break;
          }
    }
  for (i = 0; i < n; i++)
    if (nom[i]) ndr++;
  cout << ndr << endl;
  for (i = 0; i < n; i++)
    if (nom[i]) cout << A[i] << "   " << B[i] << endl;
  return 0;
}
