#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000 + 7;
const double esp = 1e-13;
map<pair<string, string>, int> f;
int n, d, s[1000 + 10];
string a[1000 + 10], b[1000 + 10], a1, b1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cin >> n >> d;
  for (long long i = 1; i <= n; i++) cin >> a[i] >> b[i] >> s[i];
  for (long long i = 1; i <= n; i++)
    for (long long j = i + 1; j <= n; j++)
      if (a[i] == b[j] && a[j] == b[i] && abs(s[i] - s[j]) <= d &&
          abs(s[i] - s[j]) > 0) {
        a1 = a[i];
        b1 = a[j];
        if (a1 > b1) swap(a1, b1);
        f[{a1, b1}]++;
      }
  cout << f.size() << "\n";
  for (typeof((f).begin()) it = (f).begin(); it != (f).end(); it++)
    cout << it->first.first << " " << it->first.second << "\n";
}
