#include <bits/stdc++.h>
using namespace std;
int d, n;
map<pair<string, string>, int> last_message;
set<pair<string, string> > ans;
set<pair<string, string> > ans2;
vector<pair<pair<string, string>, int> > r;
vector<pair<pair<string, string>, int> > request;
int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    pair<pair<string, string>, int> buf;
    cin >> buf.first.first >> buf.first.second >> buf.second;
    last_message[make_pair(buf.first.first, buf.first.second)] = -1e9;
    last_message[make_pair(buf.first.second, buf.first.first)] = -1e9;
    r.push_back(buf);
  }
  int i = 0;
  while (i < n) {
    request.clear();
    int curtime = r[i].second;
    int j = i;
    while (r[j].second == curtime) {
      request.push_back(r[j]);
      j++;
      if (j >= n) break;
    }
    i += request.size();
    for (int j = 0; j < (int)request.size(); j++) {
      if (-last_message[make_pair(request[j].first.second,
                                  request[j].first.first)] +
              curtime <=
          d) {
        if (!ans2.count(
                make_pair(request[j].first.second, request[j].first.first))) {
          ans.insert(
              make_pair(request[j].first.second, request[j].first.first));
          ans2.insert(
              make_pair(request[j].first.second, request[j].first.first));
          ans2.insert(
              make_pair(request[j].first.first, request[j].first.second));
        }
      }
    }
    for (int j = 0; j < (int)request.size(); j++) {
      last_message[make_pair(request[j].first.first, request[j].first.second)] =
          curtime;
    }
  }
  cout << ans.size() << endl;
  for (set<pair<string, string> >::iterator it = ans.begin(); it != ans.end();
       ++it) {
    pair<string, string> buf = *it;
    cout << buf.first << " " << buf.second << endl;
  }
}
