#include <bits/stdc++.h>
using namespace std;
int n, d, res;
string a[1111], b[1111];
int t[1111];
int vt[1111];
int abs_(int x) {
  if (x < 0) x *= -1;
  return x;
}
bool bang(int i, int j) {
  if (a[i].length() != b[j].length()) return 0;
  if (b[i].length() != a[j].length()) return 0;
  if (a[i] != b[j]) return 0;
  if (b[i] != a[j]) return 0;
  return 1;
}
bool bang2(int i, int j) {
  if (a[i].length() != a[j].length()) return 0;
  if (b[i].length() != b[j].length()) return 0;
  if (a[i] != a[j]) return 0;
  if (b[i] != b[j]) return 0;
  return 1;
}
bool ok(int i, int j) {
  return (bang(i, j) && (0 < abs_(t[i] - t[j])) && (abs_(t[i] - t[j]) <= d));
}
bool not_be(int i) {
  for (int k = 1; k <= res; k++)
    if (bang(i, vt[k]) || bang2(i, vt[k])) return 0;
  return 1;
}
int main() {
  cin >> n >> d;
  res = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> t[i];
    for (int j = 1; j <= i - 1; j++)
      if (ok(i, j) && not_be(i)) {
        res++;
        vt[res] = i;
        break;
      }
  }
  cout << res << endl;
  for (int i = 1; i <= res; i++) cout << a[vt[i]] << " " << b[vt[i]] << endl;
  return 0;
}
