#include <bits/stdc++.h>
using namespace std;
map<pair<string, string>, int> mp;
int t[2000];
string s[2][2000];
int main() {
  int n, d, i, j;
  mp.clear();
  scanf("%d %d", &n, &d);
  string te[2];
  int val1, val2;
  set<pair<string, string> > st;
  for (i = 0; i < n; i++) {
    cin >> s[0][i] >> s[1][i];
    scanf("%d", &t[i]);
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < i; j++) {
      if ((t[i] - t[j]) > 0 && (t[i] - t[j]) <= d && s[0][i] == s[1][j] &&
          s[0][j] == s[1][i]) {
        te[0] = s[0][i];
        te[1] = s[1][i];
        sort(te, te + 2);
        st.insert(make_pair(te[0], te[1]));
      }
    }
  }
  printf("%d\n", st.size());
  for (set<pair<string, string> >::iterator it = st.begin(); it != st.end();
       it++) {
    pair<string, string> sp = *it;
    cout << sp.first << " " << sp.second << endl;
  }
  return 0;
}
