#include <bits/stdc++.h>
using namespace std;
const int Mn = 1000 + 100;
map<pair<string, string>, int> Map;
vector<int> v[Mn];
vector<pair<string, string> > ans;
int n, d;
pair<string, string> who[Mn];
void find_friend(int x) {
  for (int i = 0; i < x; i++) {
    pair<string, string> p = make_pair(who[i].second, who[i].first);
    if (who[x] == p) {
      for (int l = 0; l < v[x].size(); l++) {
        for (int j = 0; j < v[i].size(); j++) {
          int kiana = abs(v[x][l] - v[i][j]);
          if (kiana <= d && kiana > 0) {
            ans.push_back(who[x]);
            return;
          }
        }
      }
    }
  }
}
int main() {
  int MAX_MAP = 0;
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    pair<string, string> p;
    cin >> p.first >> p.second;
    if (Map[p] == 0) {
      MAX_MAP++;
      Map[p] = MAX_MAP;
      who[MAX_MAP] = p;
    }
    int x;
    cin >> x;
    v[Map[p]].push_back(x);
  }
  for (int i = 1; i <= MAX_MAP; i++) {
    find_friend(i);
  }
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].first << " " << ans[i].second << endl;
  }
  return 0;
}
