#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d;
  cin >> n >> d;
  vector<string> x, y;
  vector<int> s(n, 0);
  string a[n], b[n];
  int c[n];
  for (int i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i];
  int count = 0;
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 0)
      for (int j = i + 1; j < n; j++) {
        if (b[i] == a[j] && a[i] == b[j] && s[j] == 0)
          if (0 < (c[j] - c[i]) && (c[j] - c[i]) <= d) {
            int flag = 0;
            for (int k = 0; k < x.size(); k++) {
              if ((a[i] == x[k] && b[i] == y[k]) ||
                  (a[i] == y[k] && b[i] == x[k])) {
                flag = 1;
                break;
              }
            }
            if (flag == 0) {
              x.push_back(a[i]);
              y.push_back(b[i]);
              s[i] = 1;
              count++;
              break;
            }
          }
      }
  }
  cout << count << "\n";
  for (int i = 0; i < x.size(); i++) cout << y[i] << " " << x[i] << "\n";
  return 0;
}
