#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d, i, j;
  cin >> n >> d;
  set<pair<string, string> > st;
  vector<pair<pair<string, string>, int> > v(n);
  for (i = 0; i < n; i++) {
    string x, y;
    int z;
    cin >> x >> y >> z;
    v[i].first = make_pair(x, y);
    v[i].second = z;
    for (j = i - 1; j >= 0; j--) {
      if (v[j].second + d < z) break;
      if (v[j].second - z == 0) continue;
      if (v[j].first == make_pair(y, x)) {
        pair<string, string> a, b;
        a = make_pair(x, y);
        b = make_pair(y, x);
        if (a < b)
          st.insert(a);
        else
          st.insert(b);
        break;
      }
    }
  }
  int m = st.size();
  set<pair<string, string> >::iterator p;
  cout << st.size() << endl;
  for (p = st.begin(); p != st.end(); p++) {
    pair<string, string> x;
    x = *p;
    cout << x.first << " " << x.second << endl;
  }
  return 0;
}
