#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
map<pair<string, string>, int> mp;
map<pair<string, string>, int> mp2;
set<pair<string, string> > ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  srand(__rdtsc());
  int n, d;
  cin >> n >> d;
  string s1, s2;
  int t;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2 >> t;
    t++;
    pair<string, string> p1 = make_pair((s1), (s2));
    pair<string, string> p2 = make_pair((s2), (s1));
    if (mp[p1] != t) mp2[p1] = mp[p1];
    mp[p1] = t;
    if ((mp[p2] != 0 && (mp[p1] - mp[p2] > 0 && mp[p1] - mp[p2] <= d)) ||
        (mp2[p2] != 0 && (mp[p1] - mp2[p2] > 0 && mp[p1] - mp2[p2] <= d))) {
      ans.insert(min(p1, p2));
    }
  }
  cout << ans.size() << endl;
  for (pair<string, string> p : ans) {
    cout << p.first << " " << p.second << endl;
  }
  return 0;
}
