#include <bits/stdc++.h>
using namespace std;
bool sortc(pair<string, pair<string, long long> > a,
           pair<string, pair<string, long long> > b) {
  return a.second.second < b.second.second;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, d, i, j, temp;
  cin >> n >> d;
  unordered_map<string, int> mymap;
  vector<pair<string, pair<string, long long> > > vect;
  vector<pair<string, string> > ans;
  string str1, str2;
  long long time;
  for (i = 0; i < n; i++) {
    cin >> str1 >> str2 >> time;
    vect.push_back(make_pair(str1, make_pair(str2, time)));
  }
  for (i = 0; i < n; i++) {
    for (j = i; j < n; j++) {
      if ((vect[i].first == vect[j].second.first &&
           vect[i].second.first == vect[j].first) &&
          (vect[j].second.second - vect[i].second.second <= d &&
           vect[j].second.second != vect[i].second.second)) {
        if (mymap[vect[i].first + vect[i].second.first] == 0) {
          mymap[vect[i].first + vect[i].second.first] = 1;
          mymap[vect[i].second.first + vect[i].first] = 1;
          ans.push_back(make_pair(vect[i].first, vect[i].second.first));
        }
        break;
      }
    }
  }
  temp = ans.size();
  cout << temp << "\n";
  for (i = 0; i < temp; i++)
    cout << ans[i].first << " " << ans[i].second << "\n";
  sort(vect.begin(), vect.end(), sortc);
}
