#include <bits/stdc++.h>
using namespace std;
struct people {
  string a, b;
  int x;
} hello[1005];
int main() {
  int n, d;
  scanf("%d%d", &n, &d);
  vector<int> xx[1004];
  map<pair<string, string>, int> names;
  int s = 1;
  for (int w = 0; w < n; w++) {
    cin >> hello[w].a >> hello[w].b >> hello[w].x;
    if (names[make_pair(hello[w].a, hello[w].b)] == 0) {
      names[make_pair(hello[w].a, hello[w].b)] = s++;
      xx[s - 1].push_back(hello[w].x);
    } else
      xx[names[make_pair(hello[w].a, hello[w].b)]].push_back(hello[w].x);
  }
  int k = 0;
  bool jay = 0;
  vector<pair<string, string> > ans;
  bool process[s + 5];
  memset(process, 0, sizeof process);
  for (int w = 0; w < n; w++) {
    jay = 0;
    if (xx[names[make_pair(hello[w].a, hello[w].b)]].size() != 0 &&
        xx[names[make_pair(hello[w].b, hello[w].a)]].size() != 0 &&
        process[names[make_pair(hello[w].a, hello[w].b)]] == 0) {
      for (int i = 0; i < xx[names[make_pair(hello[w].a, hello[w].b)]].size();
           i++) {
        for (int q = 0; q < xx[names[make_pair(hello[w].b, hello[w].a)]].size();
             q++) {
          if (abs(xx[names[make_pair(hello[w].a, hello[w].b)]][i] -
                  xx[names[make_pair(hello[w].b, hello[w].a)]][q]) <= d &&
              abs(xx[names[make_pair(hello[w].a, hello[w].b)]][i] -
                  xx[names[make_pair(hello[w].b, hello[w].a)]][q]) != 0) {
            k++;
            process[names[make_pair(hello[w].a, hello[w].b)]] = 1;
            process[names[make_pair(hello[w].b, hello[w].a)]] = 1;
            jay = 1;
            ans.push_back(make_pair(hello[w].a, hello[w].b));
            break;
          }
        }
        if (jay == 1) break;
      }
    }
  }
  cout << k << endl;
  for (int w = 0; w < ans.size(); w++) {
    cout << ans[w].first << " " << ans[w].second << endl;
  }
  return 0;
}
