#include <bits/stdc++.h>
using namespace std;
map<pair<string, string>, set<int> > mark;
map<pair<string, string>, bool> ok;
int32_t main() {
  int n, d;
  cin >> n >> d;
  vector<pair<string, string> > vec;
  for (int i = 0; i < n; i++) {
    string s, t;
    cin >> s >> t;
    int tim;
    cin >> tim;
    pair<string, string> x = make_pair(t, s);
    pair<string, string> y = make_pair(s, t);
    auto it = mark[x].upper_bound(tim);
    if (it != mark[x].end()) {
      if (abs(*it - tim) <= d) {
        vec.push_back(x);
        continue;
      }
    }
    it = mark[x].lower_bound(tim);
    if (mark[x].size() && it != mark[x].begin()) {
      it--;
      if (abs(*it - tim) <= d) {
        vec.push_back(x);
        continue;
      }
    }
    mark[y].insert(tim);
  }
  sort(vec.begin(), vec.end());
  vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
  vector<pair<string, string> > ans;
  for (auto p : vec) {
    pair<string, string> x = make_pair(p.second, p.first);
    if (!ok[p] && !ok[x]) ans.push_back(p), ok[p] = 1, ok[x] = 1;
  }
  cout << ans.size() << endl;
  for (auto x : ans) {
    cout << x.first << ' ' << x.second << endl;
  }
  return 0;
}
