#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d;
  set<pair<string, string> > s;
  set<pair<string, string> >::iterator is;
  int arr[1000];
  cin >> n >> d;
  vector<pair<string, string> > p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (p[i].first == p[j].second && p[i].second == p[j].first &&
          abs(arr[i] - arr[j]) <= d && abs(arr[i] - arr[j]) > 0) {
        if (s.find(make_pair(p[i].second, p[i].first)) == s.end()) {
          s.insert(make_pair(p[i].first, p[i].second));
        } else
          continue;
      }
    }
  }
  cout << s.size() << endl;
  for (is = s.begin(); is != s.end(); is++) {
    cout << is->first << " " << is->second << endl;
  }
}
