#include <bits/stdc++.h>
using namespace std;
const int N = 1111;
int n;
string a[N], b[N];
int t[N];
int d;
std::set<pair<string, string> > res;
int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) cin >> a[i] >> b[i] >> t[i];
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      if (!(a[i] == b[j] && a[j] == b[i])) continue;
      int m = min(t[i], t[j]);
      int M = max(t[i], t[j]);
      if (M - m <= 0 || M - m > d) continue;
      res.insert(make_pair(a[i], b[i]));
      res.insert(make_pair(b[i], a[i]));
    }
  cout << res.size() / 2 << endl;
  for (set<pair<string, string> >::iterator it = res.begin(); it != res.end();
       ++it) {
    string s1 = it->first, s2 = it->second;
    if (s1 < s2) cout << s1 << " " << s2 << endl;
  }
}
