#include <bits/stdc++.h>
using namespace std;
vector<string> i2n;
map<string, int> n2i;
vector<int> f, t, w;
vector<pair<int, int> > ans;
char was[2001][2001] = {{
    0,
}};
int main() {
  int n, d;
  cin >> n >> d;
  for (int _n = n, i = 0; i < _n; ++i) {
    string s;
    cin >> s;
    if (n2i.find(s) == n2i.end()) {
      n2i.insert(make_pair(s, i2n.size()));
      i2n.push_back(s);
    }
    int a = n2i[s], tt;
    cin >> s >> tt;
    if (n2i.find(s) == n2i.end()) {
      n2i.insert(make_pair(s, i2n.size()));
      i2n.push_back(s);
    }
    int b = n2i[s];
    if (was[a][b] || was[b][a]) continue;
    for (int _n = i, j = 0; j < _n; ++j)
      if (f[j] == b && t[j] == a && tt - w[j] > 0 && tt - w[j] <= d) {
        was[a][b] = was[b][a] = true;
        ans.push_back(make_pair(a, b));
        break;
      }
    f.push_back(a);
    t.push_back(b);
    w.push_back(tt);
  }
  cout << ans.size() << endl;
  for (__typeof((ans).begin()) it = ((ans).begin()); it != (ans).end(); ++it)
    cout << i2n[it->first] << ' ' << i2n[it->second] << endl;
  return 0;
}
