#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  std::ios::sync_with_stdio(false);
  set<pair<string, string> > S;
  int n, d, t[100001];
  string a[100001], b[100001];
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> t[i];
  }
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      if (a[i] == b[j] && a[j] == b[i] && t[j] - t[i] <= d && t[j] != t[i]) {
        S.insert({max(a[i], b[i]), min(a[i], b[i])});
      }
    }
  cout << S.size() << endl;
  set<pair<string, string> >::iterator i = S.begin();
  for (i; i != S.end(); ++i) cout << i->first << " " << i->second << endl;
}
