#include <bits/stdc++.h>
using namespace std;
int t[1001];
string A[1001], B[1001];
set<pair<string, string> > s;
int main() {
  int n, d;
  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 = 0; j < n; j++)
      if (B[i] == A[j] && B[j] == A[i] && abs(t[i] - t[j]) <= d &&
          abs(t[i] - t[j]) > 0) {
        s.insert(make_pair(min(A[i], B[i]), max(A[i], B[i])));
      }
  cout << s.size() << endl;
  for (set<pair<string, string> >::iterator it = s.begin(); it != s.end(); it++)
    cout << it->first << ' ' << it->second << endl;
}
