#include <bits/stdc++.h>
#pragma comment(linker, '/STACK:200000')
const int INF = 1e9 + 7;
const double EPS = 1e-6;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int N = 1e5 + 1000;
using namespace std;
map<pair<string, string>, set<int> > data;
int n, k;
string s, t;
set<pair<string, string> > ans;
int main() {
  cin >> n >> k;
  int v;
  for (int i = 0; i < n; i++) {
    cin >> s >> t >> v;
    auto tt = data[make_pair(t, s)].lower_bound(v - k);
    int cur = *tt;
    if (cur >= v - k && tt != data[make_pair(t, s)].end() && v != cur)
      ans.insert(make_pair(min(s, t), max(s, t)));
    data[make_pair(s, t)].insert(v);
  }
  cout << (int)ans.size() << "\n";
  for (auto it : ans) cout << it.first << " " << it.second << "\n";
  return 0;
}
