#include <bits/stdc++.h>
using namespace std;
struct format {
  string a, b;
  int t;
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed;
  cout << setprecision(10);
  int n, d;
  cin >> n >> d;
  format store[n];
  set<pair<string, string> > ans;
  for (int i = 0; i < int(n); ++i) {
    cin >> store[i].a;
    cin >> store[i].b;
    cin >> store[i].t;
  }
  for (int i = 0; i < int(n - 1); ++i) {
    for (int j = i + 1; j < n; j++) {
      string temp1, temp2, temp3, temp4;
      temp1 = store[i].a, temp2 = store[i].b, temp3 = store[j].a,
      temp4 = store[j].b;
      if (temp1 == temp4 && temp2 == temp3 &&
          abs(store[i].t - store[j].t) <= d &&
          abs(store[i].t - store[j].t) > 0) {
        pair<string, string> x = make_pair(store[i].a, store[i].b);
        pair<string, string> y = make_pair(store[i].b, store[i].a);
        if (ans.find(y) == ans.end()) ans.insert(x);
      }
    }
  }
  cout << ans.size() << "\n";
  for (auto const &x : ans) {
    cout << x.first << " " << x.second << "\n";
  }
  return 0;
}
