#include <bits/stdc++.h>
using namespace std;
map<pair<string, string>, int> m, m1;
vector<pair<string, string> > ans;
int main() {
  string s1, s2;
  int n, t, i, d, j;
  scanf("%d%d", &n, &d);
  for (i = 1; i <= n; i++) {
    cin >> s1 >> s2 >> t;
    for (j = 0; j < ans.size(); j++)
      if ((ans[j].first == s1 && ans[j].second == s2) ||
          (ans[j].first == s2 && ans[j].second == s1))
        break;
    if (j != int(ans.size())) continue;
    t++;
    if ((m[make_pair(s2, s1)] != 0 && t - m[make_pair(s2, s1)] <= d &&
         t - m[make_pair(s2, s1)] > 0) ||
        (m1[make_pair(s2, s1)] != 0 && t - m1[make_pair(s2, s1)] <= d &&
         t - m1[make_pair(s2, s1)] > 0))
      ans.push_back(make_pair(s1, s2));
    if (m[make_pair(s1, s2)] != t) m1[make_pair(s1, s2)] = m[make_pair(s1, s2)];
    m[make_pair(s1, s2)] = t;
  }
  printf("%d", int(ans.size()));
  for (i = 0; i < ans.size(); i++)
    cout << endl << ans[i].first << ' ' << ans[i].second;
  return 0;
}
