#include <bits/stdc++.h>
using namespace std;
int main() {
  long n, d, i, j, k, c[1209], s = 0;
  string a[1209], b[1209], l[1209], r[1209];
  bool t;
  cin >> n >> d;
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
  }
  for (i = 0; i < n - 1; i++) {
    for (j = i + 1; j < n; j++) {
      if (b[i] == a[j] && a[i] == b[j] && a[j] != " " && b[j] != " " &&
          a[i] != " " && b[i] != " ") {
        t = 1;
        for (k = 0; k < s; k++) {
          if (l[k] + r[k] == b[i] + b[j] || l[k] + r[k] == b[j] + b[i]) {
            t = 0;
            break;
          }
        }
        if (c[j] - c[i] <= d && c[j] != c[i] && t) {
          l[s] = b[i];
          r[s] = b[j];
          s++;
          b[j] = " ";
          a[j] = " ";
          break;
        }
      }
    }
  }
  cout << s << endl;
  for (i = 0; i < s; i++) {
    cout << l[i] << " " << r[i] << endl;
  }
}
