#include <bits/stdc++.h>
using namespace std;
long long Hash(char* s, int sz) {
  const int p = 31;
  long long hash = 0, p_pow = 1;
  for (size_t i = 0; i < sz; ++i) {
    hash += (s[i] - 'a' + 1) * p_pow;
    p_pow *= p;
  }
  return hash;
}
int gcd(int a, int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
char name[1500][50];
int main() {
  int n, d;
  scanf("%d %d", &n, &d);
  int ra = 0;
  vector<pair<pair<long long, long long>, int> > g;
  int res = 0;
  map<pair<long long, long long>, bool> used;
  for (int i = 0; i < n; i++) {
    char c;
    scanf("%c", &c);
    char s1[50];
    char s2[30];
    int q = 0;
    int l1, l2;
    while (true) {
      scanf("%c", &c);
      if (c == ' ') break;
      s1[q] = c;
      q++;
    }
    l1 = q;
    q = 0;
    while (true) {
      scanf("%c", &c);
      if (c == ' ') break;
      s2[q] = c;
      q++;
    }
    l2 = q;
    int t;
    scanf("%d", &t);
    t++;
    long long h1, h2;
    h1 = Hash(s1, l1);
    h2 = Hash(s2, l2);
    if (h1 > h2) {
      swap(h1, h2);
      t = -t;
    }
    for (int j = 0; j < g.size(); j++) {
      if (g[j].first.first == h1 && g[j].first.second == h2 &&
          !used[make_pair(h1, h2)]) {
        int t1, t2;
        t1 = g[j].second;
        t2 = t;
        if (t1 * t2 < 0) {
          t1 = abs(t1);
          t2 = abs(t2);
          if (abs(t1 - t2) <= d && t2 != t1) {
            res++;
            used[make_pair(h1, h2)] = true;
            s1[l1] = ' ';
            for (int w = 0; w < l2; w++) s1[l1 + w + 1] = s2[w];
            memcpy(name[ra], s1, l1 + l2 + 1);
            ra++;
          }
        }
      }
    }
    g.push_back(make_pair(make_pair(h1, h2), t));
  }
  printf("%d", res);
  for (int i = 0; i < ra; i++) printf("\n%s", name[i]);
  return 0;
}
