#include <bits/stdc++.h>
using namespace std;
const int N = 2111;
const int INF = 1000000000, mod = 1000000007;
const long long LLINF = 1000000000000000000ll;
vector<int> t[N][N];
map<string, int> m;
string name[N];
void get(const string &s, int &pos) {
  if (m.count(s)) {
    pos = m[s];
  } else {
    pos = m.size();
    name[pos] = s;
    m[s] = pos;
  }
}
int main() {
  int n, d;
  cin >> n >> d;
  string a, b;
  int tt;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    cin >> tt;
    int x, y;
    get(a, x);
    get(b, y);
    t[x][y].push_back(tt);
  }
  vector<pair<string, string> > ans;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      bool flag = 0;
      for (size_t ii = 0; ii < t[i][j].size(); ++ii) {
        for (size_t jj = 0; jj < t[j][i].size(); ++jj) {
          if (abs(t[i][j][ii] - t[j][i][jj]) <= d &&
              t[i][j][ii] != t[j][i][jj]) {
            flag = 1;
          }
        }
      }
      if (flag) {
        ans.push_back(make_pair(name[i], name[j]));
      }
    }
  }
  printf("%d\n", ans.size());
  for (size_t i = 0; i < ans.size(); ++i) {
    printf("%s %s\n", ans[i].first.c_str(), ans[i].second.c_str());
  }
  return 0;
}
