#include <bits/stdc++.h>
using namespace std;
void require(bool cond, const string& message = "Runtime error") {
  if (!cond) {
    cerr << message << endl;
    assert(false);
  }
}
void readData() {}
void solve() {
  int n, d;
  cin >> n >> d;
  vector<int> t(n);
  vector<string> a(n), b(n);
  set<pair<string, string> > ans;
  for (int i = 0; i < int(n); ++i) {
    cin >> a[i] >> b[i] >> t[i];
    for (int j = 0; j < int(i); ++j) {
      if (a[i] == b[j] && b[i] == a[j]) {
        if (t[i] - t[j] > 0 && t[i] - t[j] <= d) {
          string aa = a[i];
          string bb = b[i];
          if (aa > bb) {
            swap(aa, bb);
          }
          ans.insert(make_pair(aa, bb));
        }
      }
    }
  }
  cout << ans.size() << endl;
  for (set<pair<string, string> >::const_iterator it = ans.begin();
       it != ans.end(); ++it) {
    cout << it->first << " " << it->second << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  readData();
  solve();
  return 0;
}
