#include <bits/stdc++.h>
const int maxn = 2022;
using namespace std;
string name[maxn], s1, s2;
int n, m, p, d, u[maxn], v[maxn], t[maxn], res[2][maxn];
bool fr[maxn][maxn];
int vt(string s) {
  int i, x = 0;
  for (int i = 1; i <= m; i++)
    if (name[i] == s) return i;
  name[++m] = s;
  return m;
}
void input() {
  scanf("%d", &n);
  scanf("%d", &d);
  m = 0;
  int i, j;
  for (int i = 1; i <= n; i++) {
    cin >> s1 >> s2;
    scanf("%d", &t[i]);
    u[i] = vt(s1);
    v[i] = vt(s2);
    if (!fr[u[i]][v[i]])
      for (int j = 1; j <= i - 1; j++)
        if (u[i] == v[j] && v[i] == u[j] && t[j] < t[i] && t[i] - t[j] <= d) {
          fr[u[i]][v[i]] = 1;
          fr[v[i]][u[i]] = 1;
          p++;
          res[0][p] = u[i];
          res[1][p] = v[i];
          break;
        }
  }
}
void output() {
  printf("%d ", p);
  printf("\n");
  int i;
  for (int i = 1; i <= p; i++)
    cout << name[res[0][i]] << " " << name[res[1][i]] << endl;
}
int main() {
  input();
  output();
}
