#include <bits/stdc++.h>
using namespace std;
bool b[2000];
struct st {
  string s1;
  string s2;
  int ti;
};
int main() {
  int n;
  int d;
  vector<st> a;
  st tmp;
  pair<string, string> tmp2;
  set<pair<string, string>> result;
  scanf("%d%d", &n, &d);
  for (int i = 0; i < n; ++i) {
    cin >> tmp.s1 >> tmp.s2 >> tmp.ti;
    a.push_back(tmp);
  }
  for (int i = 0; i < n - 1; ++i) {
    if (b[i]) continue;
    for (int j = i + 1; j < n; ++j) {
      if (a[i].s1.compare(a[j].s2) == 0 && a[i].s2.compare(a[j].s1) == 0 &&
          ((a[j].ti - a[i].ti) <= d) && ((a[j].ti - a[i].ti) > 0)) {
        if (a[i].s1.compare(a[i].s2) < 0) {
          tmp2.first = a[i].s1;
          tmp2.second = a[i].s2;
        } else {
          tmp2.first = a[i].s2;
          tmp2.second = a[i].s1;
        }
        result.insert(tmp2);
        b[i] = true;
        b[j] = true;
      } else {
        if (a[j].ti - a[i].ti > d) break;
      }
    }
  }
  set<pair<string, string>>::iterator iter;
  printf("%d\n", result.size());
  for (iter = result.begin(); iter != result.end(); iter++) {
    cout << (*iter).first << " " << (*iter).second << endl;
  }
  return 0;
}
