#include <bits/stdc++.h>
using namespace std;
struct node {
  string x, y;
  node(string o, string p) { x = o, y = p; }
};
struct data {
  string x, y;
  int d;
} q[1234];
int main() {
  int n, k;
  ios::sync_with_stdio(false);
  vector<node> ans;
  cin >> n >> k;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> q[i].x >> q[i].y >> q[i].d;
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (q[j].d - q[i].d <= k && q[j].d - q[i].d > 0) {
        if (q[i].x == q[j].y && q[i].y == q[j].x) {
          int f = 0;
          for (int k = 0; k < ans.size(); k++) {
            if ((q[i].x == ans[k].x && q[i].y == ans[k].y) ||
                q[i].x == ans[k].y && q[i].y == ans[k].x) {
              f = 1;
              break;
            }
          }
          if (!f) ans.push_back(node(min(q[i].x, q[i].y), max(q[i].x, q[i].y)));
        }
      }
    }
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].x << " " << ans[i].y << endl;
  }
  return 0;
}
