#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<string> a, b;
  vector<int> c;
  int n, d;
  string ta, tb;
  int tc;
  scanf("%d %d\n", &n, &d);
  for (int i = 0; i < n; ++i) {
    cin >> ta >> tb >> tc;
    a.push_back(ta);
    b.push_back(tb);
    c.push_back(tc);
  }
  bool pal = 0, already = 0;
  vector<pair<string, string> > ans;
  for (int i = 0; i < n - 1; ++i) {
    pal = 0, already = 0;
    for (int j = i + 1; j < n; ++j) {
      if (a[i] == b[j] && b[i] == a[j] && c[j] - c[i] <= d && c[j] - c[i] > 0) {
        pal = 1;
        break;
      } else if (c[j] - c[i] > d)
        break;
    }
    if (pal) {
      for (int j = 0; j < ans.size(); ++j) {
        if (min(a[i], b[i]) == ans[j].first &&
            max(a[i], b[i]) == ans[j].second) {
          already = 1;
          break;
        }
      }
      if (!already) ans.push_back(make_pair(min(a[i], b[i]), max(a[i], b[i])));
    }
  }
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i].first << " " << ans[i].second << endl;
  }
  return 0;
}
