#include <bits/stdc++.h>
using namespace std;
set<pair<string, string> > m;
int n, z[2000], d, k, ans;
string a[2000], b[2000], zx[2000][5];
int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cin >> b[i];
    cin >> z[i];
  }
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      if (z[j] - z[i] <= d) {
        if (a[i] == b[j] && a[j] == b[i] && z[i] != z[j])
          m.insert(make_pair(min(a[i], b[i]), max(a[i], b[i])));
      } else
        break;
  cout << m.size() << endl;
  for (set<pair<string, string> >::iterator i = m.begin(); i != m.end(); i++)
    cout << i->first << " " << i->second << endl;
  return 0;
}
