#include <bits/stdc++.h>
using namespace std;
const int razm = 1001;
int n, d, z = 0, tek = 0;
string a[razm][3];
int b[razm];
bool v[razm];
int main() {
  cin >> n >> d;
  for (int i = 1; i <= n; i++) cin >> a[i][1] >> a[i][2] >> b[i];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (a[i][1] == a[j][2] & a[i][2] == a[j][1] & b[j] - b[i] <= d &
          b[j] > b[i] & (!v[i])) {
        bool f = true;
        for (int h = 1; h <= n; h++)
          if (v[h] & ((a[h][1] == a[i][2] & a[h][2] == a[i][1]) ||
                      (a[h][1] == a[i][1] & a[h][2] == a[i][2]))) {
            f = false;
            break;
          }
        if (f) {
          z += 1;
          v[i] = true;
        }
      }
  cout << z << endl;
  for (int i = 1; i <= n; i++)
    if (v[i]) cout << a[i][1] << ' ' << a[i][2] << endl;
  cin >> n;
}
