#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
struct edge {
  int to, cost;
  edge() { to = cost = 0; }
  edge(int to_, int cost_) {
    to = to_;
    cost = cost_;
  }
};
int n, d, t;
map<string, int> cg;
string name[2005];
int q = 0;
vector<edge> v[2005];
bool ans[2005][2005];
int answer[2005][2];
int cnt = 0;
bool answered(int timeA, int timeB) {
  int sub = timeB - timeA;
  if (sub <= 0) return false;
  if (sub > d) return false;
  return true;
}
int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    int ind[2];
    ind[0] = ind[1] = -1;
    for (int j = 0; j < 2; j++) {
      string temp;
      cin >> temp;
      if (cg.find(temp) == cg.end()) {
        cg.insert(make_pair(temp, q));
        name[q] = temp;
        q++;
      }
      ind[j] = cg[temp];
    }
    cin >> t;
    edge e(ind[1], t);
    v[ind[0]].push_back(e);
  }
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < v[i].size(); j++) {
      int from = i, to = v[i][j].to, timeA = v[i][j].cost;
      for (int k = 0; k < v[to].size(); k++) {
        if (v[to][k].to == from) {
          if (answered(timeA, v[to][k].cost))
            ans[from][to] = ans[to][from] = true;
        }
      }
    }
  }
  for (int i = 0; i < q; i++) {
    for (int j = i + 1; j < q; j++) {
      if (ans[i][j]) {
        answer[cnt][0] = i;
        answer[cnt][1] = j;
        cnt++;
      }
    }
  }
  cout << cnt << endl;
  for (int i = 0; i < cnt; i++)
    cout << name[answer[i][0]] << " " << name[answer[i][1]] << endl;
  return 0;
}
