#include <bits/stdc++.h>
using namespace std;
map<string, int> ma;
bool used[2005][2005];
int num = 1;
struct S {
  int x, y, d;
} s[1005];
string name[2005];
int main() {
  int N, D;
  int i, j, k;
  int res = 0;
  cin >> N >> D;
  string s1, s2, d;
  for (i = 0; i < N; i++) {
    cin >> s1 >> s2 >> s[i].d;
    int t1 = ma[s1], t2 = ma[s2];
    if (t1 == 0) {
      ma[s1] = t1 = num;
      name[num++] = s1;
    }
    if (t2 == 0) {
      ma[s2] = t2 = num;
      name[num++] = s2;
    }
    s[i].x = t1, s[i].y = t2;
    if (used[t1][t2]) continue;
    for (j = i - 1; j >= 0; j--) {
      if (s[i].d - s[j].d > D)
        break;
      else if (s[i].d == s[j].d)
        continue;
      else if (s[j].x == s[i].y && s[j].y == s[i].x) {
        res++;
        used[t1][t2] = used[t2][t1] = 1;
        break;
      }
    }
  }
  cout << res << endl;
  for (i = 1; i < num; i++) {
    for (j = i + 1; j < num; j++) {
      if (used[i][j]) {
        cout << name[i] << " " << name[j] << endl;
      }
    }
  }
}
