#include <bits/stdc++.h>
using namespace std;
struct record {
  int number;
  char nameA[21];
  char nameB[21];
  unsigned int t;
};
void search(vector<record*>* target, record* find, int d,
            vector<record*>* result) {
  for (int i = find->number; i < (*target).size(); i++) {
    if ((strcmp((*target).at(i)->nameA, find->nameB) == 0) &&
        (strcmp((*target).at(i)->nameB, find->nameA) == 0)) {
      if (((*target).at(i)->t - find->t <= d) &&
          ((*target).at(i)->t - find->t > 0)) {
        (*result).push_back(find);
        break;
      }
    }
    if ((*target).at(i)->t - find->t > d) break;
  }
}
int main() {
  int n, d;
  cin >> n >> d;
  vector<record*> journal;
  vector<record*> result;
  unsigned int iteratorJournal;
  vector<record*>::iterator iteratorResult;
  char tempA[21], tempB[21];
  unsigned int tempT;
  for (int i = 0; i < n; i++) {
    cin >> tempA >> tempB >> tempT;
    record* temp = new record;
    strcpy(temp->nameA, tempA);
    strcpy(temp->nameB, tempB);
    temp->t = tempT;
    temp->number = i;
    journal.push_back(temp);
  }
  for (iteratorJournal = 0; iteratorJournal < journal.size();
       iteratorJournal++) {
    search(&journal, journal.at(iteratorJournal), d, &result);
  }
  int l = 0;
  while (l < result.size()) {
    for (int i = l + 1; i < result.size(); i++) {
      if (((strcmp(result.at(l)->nameA, result.at(i)->nameB) == 0) &&
           (strcmp(result.at(l)->nameB, result.at(i)->nameA) == 0)) ||
          ((strcmp(result.at(l)->nameA, result.at(i)->nameA) == 0) &&
           (strcmp(result.at(l)->nameB, result.at(i)->nameB) == 0))) {
        result.erase(result.begin() + i);
        i--;
      }
    }
    l++;
  }
  cout << result.size() << endl;
  for (int k = 0; k < result.size(); k++) {
    cout << result.at(k)->nameA << " " << result.at(k)->nameB << endl;
  }
  return 0;
}
