#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d, s = 0;
  cin >> n >> d;
  vector<pair<pair<string, string>, int> > v;
  set<pair<string, string> > friends;
  vector<pair<string, string> > ans;
  for (int i = 0; i < n; i++) {
    string s1, s2;
    int t;
    cin >> s1 >> s2 >> t;
    for (int j = 0; j < int(v.size()); j++)
      if (v[j].first.first == s2 && v[j].first.second == s1 &&
          v[j].second != t && t - v[j].second <= d) {
        int masksize = int(friends.size());
        friends.insert(make_pair(min(s1, s2), max(s1, s2)));
        if (masksize != int(friends.size())) {
          ans.push_back(make_pair(s1, s2));
          s++;
          friends.insert(make_pair(s1, s2));
        }
      }
    v.push_back(make_pair(make_pair(s1, s2), t));
  }
  cout << s << endl;
  for (int i = 0; i < s; i++)
    cout << ans[i].first << ' ' << ans[i].second << endl;
  return 0;
}
