#include<bits/stdc++.h>
using namespace std;
struct Node{
int time;
string a,b;
};
map<string,int>last;
map<string,int>last_send;
vector<pair<string,string>>friends;
map<pair<string,string>,bool>vis;
int main() {
int n,d;
scanf("%d%d",&n,&d);
for(int i=1;i<=n;i++){
Node node;
cin>>node.a>>node.b;
scanf("%d",&node.time);
if(node.a==node.b)continue;
if(!last.count(node.b))last[node.b]=INT_MIN;
if(last[node.b]>=node.time && last_send[node.b]!=node.time){
if (!vis[make_pair(node.a, node.b)]) {
vis[make_pair(node.a, node.b)] = true;
vis[make_pair(node.b, node.a)] = true;
friends.push_back(make_pair(node.a, node.b));
}
}
last[node.a] = node.time+d;
last_send[node.a]=node.time;
}
cout<<friends.size()<<endl;
for(auto i: friends){
cout<<i.first<<" "<<i.second<<endl;
}
return 0;
}