#include <bits/stdc++.h>
using namespace std;
long long pwr(long long a, long long b, long long mod) {
  a %= mod;
  if (a < 0) a += mod;
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return ans;
}
long long pwr(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans *= a;
    a *= a;
    b /= 2;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  while (b) {
    long long temp = a;
    a = b;
    b = temp % b;
  }
  return a;
}
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
long long modularInverse(long long a, long long m) {
  assert(false);
  return pwr(a, m - 2, m);
}
const int mod = 1000000007;
string a[1001];
string b[1001];
int t[1001];
int main() {
  std::ios::sync_with_stdio(false);
  int n, d;
  cin >> n >> d;
  set<pair<string, string> > f;
  for (int i = 0; i < n; ++i) {
    cin >> a[i] >> b[i] >> t[i];
    for (int j = 0; j < i; ++j) {
      if (t[i] - t[j] <= d && t[i] - t[j] > 0 && a[i] == b[j] && b[i] == a[j]) {
        if (a[i] > b[i])
          f.insert(make_pair(a[i], b[i]));
        else
          f.insert(make_pair(b[i], a[i]));
      }
    }
  }
  cout << f.size() << endl;
  for (set<pair<string, string> >::iterator i = f.begin(); i != f.end(); ++i) {
    cout << i->first << " " << i->second << "\n";
  }
}
