#include <bits/stdc++.h>
using namespace std;
string a, b;
int n, d;
map<string, int> id;
int m;
string fromid[2000];
int getid(string &s) {
  if (((id).find(s) != (id).end())) {
    return id[s];
  } else {
    fromid[m] = s;
    return id[s] = m++;
  }
}
struct CMD {
  int a, b, t;
} C[2000];
int lst[2000][2000];
bool mat[2000][2000];
int main() {
  ios_base::sync_with_stdio(0);
  while (cin >> n >> d) {
    m = 0;
    id.clear();
    for (int i = 0; i < n; i++) {
      string A, B;
      int a, b, t;
      cin >> A >> B >> t;
      a = getid(A);
      b = getid(B);
      C[i].a = a;
      C[i].b = b;
      C[i].t = t;
    }
    for (int i = 0; i < m; i++) {
      memset(lst[i], (int)0xc0c0c0c0, sizeof(int) * m);
      memset(mat[i], false, sizeof(bool) * m);
    }
    for (int i = 0; i < n; i++) {
      if (i == 0 || C[i - 1].t < C[i].t) {
        for (int j = i; j < n && C[j].t == C[i].t; j++) {
          int a = C[j].a, b = C[j].b, t = C[j].t;
          if (t - lst[b][a] <= d && t - lst[b][a] > 0) {
            mat[a][b] = true;
          }
        }
        for (int j = i; j < n && C[j].t == C[i].t; j++) {
          int a = C[j].a, b = C[j].b, t = C[j].t;
          lst[a][b] = t;
        }
      }
    }
    int k = 0;
    for (int i = 0; i < m; i++)
      for (int j = i + 1; j < m; j++) {
        if (mat[i][j] || mat[j][i]) k++;
      }
    cout << k << "\n";
    for (int i = 0; i < m; i++)
      for (int j = i + 1; j < m; j++) {
        if (mat[i][j] || mat[j][i]) {
          cout << fromid[i] << " " << fromid[j] << "\n";
        }
      }
  }
  return 0;
}
