#include <bits/stdc++.h>
using namespace std;
int toInt(string s) {
  int r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
long long toInt64(string s) {
  long long r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
double toDouble(string s) {
  double r = 0;
  istringstream sin(s);
  sin >> r;
  return r;
}
string toString(long long n) {
  string s, s1;
  while (n / 10 > 0) {
    s += (char)((n % 10) + 48);
    n /= 10;
  }
  s += (char)((n % 10) + 48);
  n /= 10;
  s1 = s;
  for (long long i = 0; i < s.length(); i++) s1[(s.length() - 1) - i] = s[i];
  return s1;
}
bool isUpperCase(char c) { return c >= 'A' && c <= 'Z'; }
bool isLowerCase(char c) { return c >= 'a' && c <= 'z'; }
bool isLetter(char c) { return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'; }
bool isDigit(char c) { return c >= '0' && c <= '9'; }
char toLowerCase(char c) { return (isUpperCase(c)) ? (c + 32) : c; }
char toUpperCase(char c) { return (isLowerCase(c)) ? (c - 32) : c; }
vector<int> v;
map<pair<string, string>, bool> m;
int main() {
  ios::sync_with_stdio(0);
  int n, d;
  cin >> n >> d;
  string s1[1001], s2[1001];
  int t[1001], sum = 0;
  for (long long i = 0; i < n; i++) {
    cin >> s1[i] >> s2[i] >> t[i];
    for (int j = i - 1; j >= 0; j--) {
      if (t[i] - t[j] > d) break;
      if (s1[i] == s2[j] && s2[i] == s1[j] && t[i] != t[j] &&
          m.find(make_pair(s1[i], s2[i])) == m.end()) {
        sum++;
        v.push_back(i);
        m[make_pair(s1[i], s2[i])] = true;
        m[make_pair(s2[i], s1[i])] = true;
      }
    }
  }
  cout << sum << endl;
  for (long long i = 0; i < sum; i++)
    cout << s1[v[i]] << ' ' << s2[v[i]] << endl;
}
