#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T gcd(T a, T b) {
  while (b) b ^= a ^= b ^= a %= b;
  return (a);
}
int main() {
  int n, d;
  cin >> n >> d;
  vector<pair<pair<string, string>, int>> V(n);
  vector<pair<string, string>> res;
  for (int i = 0; i < n; i++) {
    cin >> V[i].first.first >> V[i].first.second >> V[i].second;
    for (int j = i - 1; j >= 0; j--)
      if (V[j].first.first == V[i].first.second &&
          V[j].first.second == V[i].first.first) {
        int tmp = V[i].second - V[j].second;
        if ((0 < tmp) && (tmp <= d)) res.push_back(V[j].first);
      }
  }
  for (int i = 0; i < res.size(); i++)
    if (res[i].first > res[i].second) swap(res[i].first, res[i].second);
  sort((res).begin(), (res).end());
  res.erase(unique((res).begin(), (res).end()), res.end());
  cout << res.size() << endl;
  for (int i = 0; i < res.size(); i++)
    cout << res[i].first << " " << res[i].second << endl;
  return (0);
}
