#include <bits/stdc++.h>
using namespace std;
class Pair {
 public:
  string first, second;
  int t;
  bool x;
  void write() {
    cin >> first >> second >> t;
    x = true;
    return;
  }
  void out() { cout << endl << first << " " << second; }
  Pair& operator=(Pair& rhs) {
    first = rhs.first;
    second = rhs.second;
    t = rhs.t;
  }
  bool operator==(Pair& rhs) {
    if (first == rhs.first && second == rhs.second)
      return true;
    else
      return false;
  }
  int operator-(Pair& rhs) { return t - rhs.t; }
  bool operator!=(Pair& rhs) {
    if (t != rhs.t)
      return true;
    else
      return false;
  }
};
int main() {
  int n, num = 0, d;
  cin >> n >> d;
  Pair Pr[n];
  for (int i = 0; i < n; i++) Pr[i].write();
  for (int i = 0; i < n; i++)
    if (Pr[i].x) {
      bool b = true;
      Pair A = Pr[i], B = A;
      swap(B.first, B.second);
      B.t = -2000;
      Pair A1 = A, B1 = B;
      for (int j = i + 1; j < n && b; j++) {
        if (A == Pr[j] || B == Pr[j]) {
          if (A == Pr[j]) {
            if ((Pr[j] - B <= d && Pr[j] - B != 0) ||
                (Pr[j] - B1 <= d && Pr[j] - B1 != 0))
              b = false;
            else {
              if (A.t != Pr[j].t) A1 = A;
              A = Pr[j];
            }
          } else if ((Pr[j] - A <= d && Pr[j] - A != 0) ||
                     (Pr[j] - A1 <= d && Pr[j] - A1 != 0))
            b = false;
          else {
            if (B.t != Pr[j].t) B1 = B;
            B = Pr[j];
          }
        }
      }
      if (b)
        Pr[i].x = false;
      else
        num++;
      for (int j = i + 1; j < n; j++) {
        if (A == Pr[j] || B == Pr[j]) Pr[j].x = false;
      }
    }
  cout << num;
  for (int i = 0; i < n; i++)
    if (Pr[i].x) Pr[i].out();
  return 0;
}
