#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d;
  cin >> n >> d;
  vector<pair<string, string> > v(n), res;
  vector<int> t(n);
  for (int i = 0; i < n; i++) {
    string a, b;
    int ti;
    cin >> a >> b >> ti;
    t[i] = ti;
    v[i] = make_pair(a, b);
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (v[i].first == v[j].second && v[i].second == v[j].first) {
        if (t[j] - t[i] > 0 && t[j] - t[i] <= d) {
          pair<string, string> tmp1(v[i].first, v[i].second);
          pair<string, string> tmp2(v[i].second, v[i].first);
          if (find(res.begin(), res.end(), tmp1) == res.end() &&
              find(res.begin(), res.end(), tmp2) == res.end()) {
            res.push_back(tmp1);
          }
        }
      }
    }
  }
  cout << res.size() << endl;
  for (int i = 0; i < res.size(); i++) {
    cout << res[i].first << ' ' << res[i].second << endl;
  }
  return 0;
}
