#include<bits/stdc++.h>
using namespace std;

#define maxs 500000
#define pi pair<int,int>
#define mp make_pair
#define pb push_back

vector< vector<int> > graph(maxs);
int mark[maxs]={0};
int sys[maxs]={0};
int h[maxs]={0};

int n;
int g_ans,g_add;

void dfs(int index)
{	mark[index]=1;
	map<int,int> mymap1;
	mymap1.clear();
	map<int,int>::iterator it;
	int eq=1;
	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(mark[ch]==0)
		{	dfs(ch);
			if(sys[ch]==0)eq=0;
			if(sys[ch]==1)mymap1[h[ch]]++;
		}
	}
	if(eq==0)
	{
		sys[index]=0;
		h[index]=-1;
	}
	else if(eq==1)
	{	if(mymap1.size()>1)
		{	sys[index]=0;
			h[index]=-1;
		}
		else if(mymap1.size()==1)
		{	sys[index]=1;
			it = mymap1.begin();
			h[index] = it->first+1;
		}
		else if(mymap1.size()==0)
		{	sys[index]=1;
			h[index]=1;
		}
	}
	mymap1.clear();
}

void sdfs(int index,int p,int st,int hei)
{
	mark[index]=1;

	map<int,int> track;
	int nch = graph[index].size();
	int add=0;

	map<int,int>::iterator it;

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	add+=sys[ch];
			track[h[ch]]++;
		}
		else
		{	add+=st;
			track[hei]++;
		}
	}

	if(add==nch)
	{	int total=0;
		if(track.size()==2)
		{	g_ans=1;
			it=track.begin();
			total += it->first;
			it++;
			total += it->first;
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==1)
		{	g_ans=1;
			it=track.begin();
			if(nch==1)total=it->first;
			else if(nch>1)total=(2*(it->first));
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==0)
		{	g_ans=1;
			total = 0;
			if(g_add>total)
			{	g_add=total;
			}
		}
	}

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	int left = add - sys[ch];
			track[h[ch]]--;
			if(track[h[ch]]==0)
			{	it = track.find(h[ch]);
				track.erase(it);
			}
			if(left==nch-1)
			{	if(track.size()==1)
				{	it = track.begin();
					sdfs(ch,index,1,it->first+1);
				}
				else if(track.size()==0)
				{	sdfs(ch,index,1,1);
				}
				else if(track.size()>1)
				{	sdfs(ch,index,0,-1);
				}
			}
			else
			{	sdfs(ch,index,0,-1);
			}
			track[h[ch]]++;
		}
	}
	track.clear();
}

int totald()
{

	g_ans=0;
	g_add=10000000;
	for(int i=1;i<=n;i++)mark[i]=0;
	dfs(1);
	for(int i=1;i<=n;i++)mark[i]=0;
	sdfs(1,-1,0,-1);
	if(g_ans==0)cout<<-1<<endl;
	else if(g_ans==1)cout<<g_add<<endl;

}

int main()
{	int x,y;
	cin>>n;
	for(int i=0;i<n-1;i++)
	{	cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}
	totald();
}


#include<bits/stdc++.h>
using namespace std;

#define maxs 500000
#define pi pair<int,int>
#define mp make_pair
#define pb push_back

map<int,int>::iterator it;

vector< vector<int> > graph(maxs);
int mark[maxs]={0};
int sys[maxs]={0};
int h[maxs]={0};

int n;
int g_ans,g_add;

void dfs(int index)
{	mark[index]=1;
	map<int,int> track;

	int heq=1;

	for(int i=0;i<graph[index].size();i++)
	{	int ch=graph[index][i];
		if(mark[ch]==0)
		{	dfs(ch);
			heq = heq and sys[ch];
			if(sys[ch]==1)track[h[ch]]++;
		}
	}

	if(heq==0)
	{	sys[index]=0;
		h[index]=-1;
	}
	else if(heq==1)
	{	if(track.size()>1)
		{	sys[index]=0;
			h[index]=-1;
		}
		else if(track.size()==1)
		{	it=track.begin();
			sys[index]=1;
			h[index]=it->first+1;
		}
		else if(track.size()==0)
		{	sys[index]=1;
			h[index]=1;
		}
	}
	track.clear();
}

void sdfs(int index,int p,int st,int hei)
{
	mark[index]=1;

	map<int,int> track;
	int nch = graph[index].size();
	int add=0;

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	add+=sys[ch];
			track[h[ch]]++;
		}
		else
		{	add+=st;
			track[hei]++;
		}
	}

	if(add==nch)
	{	int total=0;
		if(track.size()==2)
		{	g_ans=1;
			it=track.begin();
			total += it->first;
			it++;
			total += it->first;
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==1)
		{	g_ans=1;
			it=track.begin();
			if(nch==1)total=it->first;
			else if(nch>1)total=(2*(it->first));
			if(g_add>total)
			{	g_add=total;
			}
		}
	}

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	int left = add - sys[ch];
			track[h[ch]]--;
			if(track[h[ch]]==0)
			{	it = track.find(h[ch]);
				track.erase(it);
			}
			if(left==nch-1)
			{	if(track.size()==1)
				{	it = track.begin();
					sdfs(ch,index,1,it->first+1);
				}
				else if(track.size()==0)
				{	sdfs(ch,index,1,1);
				}
				else if(track.size()>1)
				{	sdfs(ch,index,0,-1);
				}
			}
			else
			{	sdfs(ch,index,0,-1);
			}
			track[h[ch]]++;
		}
	}

}

int totald()
{

	g_ans=0;
	g_add=10000000;
	for(int i=1;i<=n;i++)mark[i]=0;
	dfs(1);
	for(int i=1;i<=n;i++)mark[i]=0;
	sdfs(1,-1,0,-1);
	if(g_ans==0)cout<<-1<<endl;
	else if(g_ans==1)cout<<g_add<<endl;

}

int main()
{	int x,y;
	cin>>n;
	for(int i=0;i<n-1;i++)
	{	cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}
	totald();
}


#include<bits/stdc++.h>
using namespace std;

#define maxs 500000
#define pi pair<int,int>
#define mp make_pair
#define pb push_back

vector< vector<int> > graph(maxs);
int mark[maxs]={0};
int sys[maxs]={0};
int h[maxs]={0};

int n;
int g_ans,g_add;

void dfs(int index)
{	mark[index]=1;
	map<int,int> mymap1;
	mymap1.clear();
	map<int,int>::iterator it;
	int eq=1;
	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(mark[ch]==0)
		{	dfs(ch);
			if(sys[ch]==0)eq=0;
			if(sys[ch]==1)mymap1[h[ch]]++;
		}
	}
	if(eq==0)
	{
		sys[index]=0;
		h[index]=-1;
	}
	else if(eq==1)
	{	if(mymap1.size()>1)
		{	sys[index]=0;
			h[index]=-1;
		}
		else if(mymap1.size()==1)
		{	sys[index]=1;
			it = mymap1.begin();
			h[index] = it->first+1;
		}
		else if(mymap1.size()==0)
		{	sys[index]=1;
			h[index]=1;
		}
	}
	mymap1.clear();
}

void sdfs(int index,int p,int st,int hei)
{
	mark[index]=1;

	map<int,int> track;
	int nch = graph[index].size();
	int add=0;

	map<int,int>::iterator it;

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	add+=sys[ch];
			track[h[ch]]++;
		}
		else
		{	add+=st;
			track[hei]++;
		}
	}

	if(add==nch)
	{	int total=0;
		if(track.size()==2)
		{	g_ans=1;
			it=track.begin();
			total += it->first;
			it++;
			total += it->first;
			while(total%2==0)total=total/2;
			//if(total%2==0)total=1;
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==1)
		{	g_ans=1;
			it=track.begin();
			total = it->first;
			while(total%2==0)total=total/2;
			//if(total%2==0)total=1;
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==0)
		{	g_ans=1;
			total = 0;
			if(g_add>total)
			{	g_add=total;
			}
		}
	}

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	int left = add - sys[ch];
			track[h[ch]]--;
			if(track[h[ch]]==0)
			{	it = track.find(h[ch]);
				track.erase(it);
			}
			if(left==nch-1)
			{	if(track.size()==1)
				{	it = track.begin();
					sdfs(ch,index,1,it->first+1);
				}
				else if(track.size()==0)
				{	sdfs(ch,index,1,1);
				}
				else if(track.size()>1)
				{	sdfs(ch,index,0,-1);
				}
			}
			else
			{	sdfs(ch,index,0,-1);
			}
			track[h[ch]]++;
		}
	}
	track.clear();
}

int totald()
{

	g_ans=0;
	g_add=10000000;
	for(int i=1;i<=n;i++)mark[i]=0;
	dfs(1);
	for(int i=1;i<=n;i++)mark[i]=0;
	sdfs(1,-1,0,-1);
	if(g_ans==0)cout<<-1<<endl;
	else if(g_ans==1)
	{	cout<<g_add<<endl;
	}

}

int main()
{	int x,y;
	cin>>n;
	for(int i=0;i<n-1;i++)
	{	cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}
	totald();
}


#include<bits/stdc++.h>
using namespace std;
#define maxs 500000
#define pi pair<int,int>
#define mp make_pair
#define pb push_back

map<int,int>::iterator it;

vector< vector<int> > graph(maxs);
int mark[maxs]={0};
int sys[maxs]={0};
int h[maxs]={0};

int n;

void dfs(int index)
{	mark[index]=1;
	map<int,int> mymap1;
	mymap1.clear();
	int eq=1;
	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(mark[ch]==0)
		{	dfs(ch);
			if(sys[ch]==0)eq=0;
			if(sys[ch]==1)mymap1[h[ch]]++;
		}
	}
	if(eq==0)
	{
		sys[index]=0;
		h[index]=-1;
	}
	else
	{	if(mymap1.size()>1)
		{	sys[index]=0;
			h[index]=-1;
		}
		else if(mymap1.size()==1)
		{	sys[index]=1;
			it = mymap1.begin();
			h[index] = it->first+1;
		}
		else if(mymap1.size()==0)
		{	sys[index]=1;
			h[index]=1;
		}
	}
	mymap1.clear();
}

pi dfs2(int index,int p)
{	mark[index]=1;
	int eq=1;
	map<int,int> track;
	track.clear();
	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(mark[ch]==0)
		{	pi res = dfs2(ch,index);
			if(res.first==0)eq=0;
			if(res.first==1)track[res.second]++;
		}
	}
	if(eq==0)return mp(0,-1);
	else
	{	if(track.size()>1)return mp(0,-1);
		else if(track.size()==1)
		{	it = track.begin();
			return mp(1,it->first+1);
		}
		else if(track.size()==0)
		{	return mp(1,1);
		}
	}
	track.clear();
}

void totaldfs()
{	dfs(1);
	if(sys[1]==1)
	{	cout<<h[1]-1<<endl;
		return;
	}

	map<int,int> mymap2;

	int chose=-1;
	for(int i=1;i<=n;i++)
	{	if(graph[i].size()>=3 and sys[i]==0)chose=i;
	}
	for(int i=1;i<=n;i++)
	{	mark[i]=0;
	}

	mark[chose]=1;
	int pos=1;
	mymap2.clear();
	for(int i=0;i<graph[chose].size();i++)
	{	int ch = graph[chose][i];
		pi res = dfs2(ch,chose);
		if(res.first==0)pos=0;
		if(res.first==1)mymap2[res.second]++;
	}
	if(pos==0)cout<<-1<<endl;
	else
	{	if(mymap2.size()>2)
		{	cout<<-1<<endl;
		}
		else if(mymap2.size()==2)
		{	int add=0;
			it=mymap2.begin();
			add += it->first;
			it++; add += it->first;
			cout<<add<<endl;
		}
		else if(mymap2.size()==1)
		{	int add=0;
			it=mymap2.begin();
			add = 2*(it->first);
			cout<<add<<endl;
		}
		else if(mymap2.size()==0)
		{	cout<<0<<endl;
		}
	}
	return;
}

int main()
{	int x,y;
	cin>>n;
	for(int i=0;i<n-1;i++)
	{	cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}
	for(int i=1;i<=n;i++)mark[i]=0;
	totaldfs();

}


#include<bits/stdc++.h>
using namespace std;
#define maxs 500000
#define pi pair<int,int>
#define mp make_pair
#define pb push_back

map<int,int>::iterator it;

vector< vector<int> > graph(maxs);
int mark[maxs]={0};
int sys[maxs]={0};
int h[maxs]={0};

int n;

void dfs(int index)
{	mark[index]=1;
	map<int,int> mymap1;
	mymap1.clear();
	int eq=1;
	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(mark[ch]==0)
		{	dfs(ch);
			if(sys[ch]==0)eq=0;
			if(sys[ch]==1)mymap1[h[ch]]++;
		}
	}
	if(eq==0)
	{
		sys[index]=0;
		h[index]=-1;
	}
	else if(eq==1)
	{	if(mymap1.size()>1)
		{	sys[index]=0;
			h[index]=-1;
		}
		else if(mymap1.size()==1)
		{	sys[index]=1;
			it = mymap1.begin();
			h[index] = it->first+1;
		}
		else if(mymap1.size()==0)
		{	sys[index]=1;
			h[index]=1;
		}
	}
	mymap1.clear();
}

pi dfs2(int index,int p)
{	mark[index]=1;
	int eq=1;
	map<int,int> track;
	track.clear();
	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(mark[ch]==0)
		{	pi res = dfs2(ch,index);
			if(res.first==0)eq=0;
			if(res.first==1)track[res.second]++;
		}
	}
	if(eq==0)return mp(0,-1);
	else if(eq==1)
	{	if(track.size()>1)return mp(0,-1);
		else if(track.size()==1)
		{	it = track.begin();
			return mp(1,it->first+1);
		}
		else if(track.size()==0)
		{	return mp(1,1);
		}
	}
	track.clear();
}

void totaldfs()
{
	map<int,int> mymap2;
	dfs(1);
	if(sys[1]==1)
	{	cout<<h[1]-1<<endl;
		return;
	}
	int num=0;
	for(int i=0;i<graph[1].size();i++)
	{	int ch=graph[1][i];
		num += sys[ch];
	}
	if(num==graph[1].size())
	{	mymap2.clear();
		for(int i=0;i<graph[1].size();i++)
		{	int ch = graph[1][i];
			mymap2[h[ch]]++;
		}
		if(mymap2.size()>2)
		{	cout<<-1<<endl;
		}
		else if(mymap2.size()==2)
		{	int add=0;
			it=mymap2.begin();
			add+=it->first;
			it++; add+=it->first;
			cout<<add<<endl;
		}
		else if(mymap2.size()==1)
		{	int add=0;
			it=mymap2.begin();
			add=2*(it->first);
			cout<<add<<endl;
		}
		else if(mymap2.size()==1)
		{	cout<<0<<endl;
		}
		mymap2.clear();
		return;
	}

	int chose=-1;
	for(int i=1;i<=n;i++)
	{	if(graph[i].size()>=3 and sys[i]==0)chose=i;
	}

	for(int i=1;i<=n;i++)
	{	mark[i]=0;
	}

	mark[chose]=1;

	int pos=1;
	mymap2.clear();
	for(int i=0;i<graph[chose].size();i++)
	{	int ch = graph[chose][i];
		pi res = dfs2(ch,chose);
		if(res.first==0)pos=0;
		if(res.first==1)mymap2[res.second]++;
	}
	if(pos==0)cout<<-1<<endl;
	else if(pos==1)
	{	if(mymap2.size()>2)
		{	cout<<-1<<endl;
		}
		else if(mymap2.size()==2)
		{	int add=0;
			it=mymap2.begin();
			add += it->first;
			it++; add += it->first;
			cout<<add<<endl;
		}
		else if(mymap2.size()==1)
		{	int add=0;
			it=mymap2.begin();
			add = 2*(it->first);
			cout<<add<<endl;
		}
		else if(mymap2.size()==0)
		{	cout<<0<<endl;
		}
	}
	return;
}

int main()
{	int x,y;
	cin>>n;
	for(int i=0;i<n-1;i++)
	{	cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}
	for(int i=1;i<=n;i++)mark[i]=0;
	totaldfs();

}


#include<bits/stdc++.h>
using namespace std;

#define maxs 500000
#define pi pair<int,int>
#define mp make_pair
#define pb push_back

map<int,int>::iterator it;

vector< vector<int> > graph(maxs);
int mark[maxs]={0};
int sys[maxs]={0};
int h[maxs]={0};

int n;
int g_ans,g_add;

void dfs(int index)
{	mark[index]=1;
	map<int,int> track;

	int heq=1;

	for(int i=0;i<graph[index].size();i++)
	{	int ch=graph[index][i];
		if(mark[ch]==0)
		{	dfs(ch);
			heq = heq and sys[ch];
			if(sys[ch]==1)track[h[ch]]++;
		}
	}

	if(heq==0)
	{	sys[index]=0;
		h[index]=-1;
	}
	else if(heq==1)
	{	if(track.size()>1)
		{	sys[index]=0;
			h[index]=-1;
		}
		else if(track.size()==1)
		{	it=track.begin();
			sys[index]=1;
			h[index]=it->first+1;
		}
		else if(track.size()==0)
		{	sys[index]=1;
			h[index]=1;
		}
	}
	track.clear();
}

void sdfs(int index,int p,int st,int hei)
{
	mark[index]=1;

	map<int,int> track;
	int nch = graph[index].size();
	int add=0;

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	add+=sys[ch];
			track[h[ch]]++;
		}
		else
		{	add+=st;
			track[hei]++;
		}
	}

	if(add==nch)
	{	int total=0;
		if(track.size()==2)
		{	g_ans=1;
			it=track.begin();
			total += it->first;
			it++;
			total += it->first;
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==1)
		{	g_ans=1;
			it=track.begin();
			if(nch==1)total=it->first;
			else if(nch>1)total=(2*(it->first));
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==0)
		{	g_ans=1;
			total = 0;
			if(g_add>total)
			{	g_add=total;
			}
		}
	}

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	int left = add - sys[ch];
			track[h[ch]]--;
			if(track[h[ch]]==0)
			{	it = track.find(h[ch]);
				track.erase(it);
			}
			if(left==nch-1)
			{	if(track.size()==1)
				{	it = track.begin();
					sdfs(ch,index,1,it->first+1);
				}
				else if(track.size()==0)
				{	sdfs(ch,index,1,1);
				}
				else if(track.size()>1)
				{	sdfs(ch,index,0,-1);
				}
			}
			else
			{	sdfs(ch,index,0,-1);
			}
			track[h[ch]]++;
		}
	}
	track.clear();
}

int totald()
{

	g_ans=0;
	g_add=100000000;
	for(int i=1;i<=n;i++)mark[i]=0;
	dfs(1);
	for(int i=1;i<=n;i++)mark[i]=0;
	sdfs(1,-1,0,-1);
	if(g_ans==0)cout<<-1<<endl;
	else if(g_ans==1)cout<<g_add<<endl;

}

int main()
{	int x,y;
	cin>>n;
	for(int i=0;i<n-1;i++)
	{	cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}
	totald();
}


#include<bits/stdc++.h>
using namespace std;

#define maxs 500000
#define pi pair<int,int>
#define mp make_pair
#define pb push_back

vector< vector<int> > graph(maxs);
int mark[maxs]={0};
int sys[maxs]={0};
int h[maxs]={0};

int n;
int g_ans,g_add;

void dfs(int index)
{	mark[index]=1;
	map<int,int> mymap1;
	mymap1.clear();
	map<int,int>::iterator it;
	int eq=1;
	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(mark[ch]==0)
		{	dfs(ch);
			if(sys[ch]==0)eq=0;
			if(sys[ch]==1)mymap1[h[ch]]++;
		}
	}
	if(eq==0)
	{
		sys[index]=0;
		h[index]=-1;
	}
	else if(eq==1)
	{	if(mymap1.size()>1)
		{	sys[index]=0;
			h[index]=-1;
		}
		else if(mymap1.size()==1)
		{	sys[index]=1;
			it = mymap1.begin();
			h[index] = it->first+1;
		}
		else if(mymap1.size()==0)
		{	sys[index]=1;
			h[index]=1;
		}
	}
	mymap1.clear();
}

void sdfs(int index,int p,int st,int hei)
{
	mark[index]=1;

	map<int,int> track;
	int nch = graph[index].size();
	int add=0;

	map<int,int>::iterator it;

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	add+=sys[ch];
			track[h[ch]]++;
		}
		else
		{	add+=st;
			track[hei]++;
		}
	}

	if(add==nch)
	{	int total=0;
		if(track.size()==2)
		{	g_ans=1;
			it=track.begin();
			total += it->first;
			it++;
			total += it->first;
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==1)
		{	g_ans=1;
			it=track.begin();
			total = it->first;
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==0)
		{	g_ans=1;
			total = 0;
			if(g_add>total)
			{	g_add=total;
			}
		}
	}

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	int left = add - sys[ch];
			track[h[ch]]--;
			if(track[h[ch]]==0)
			{	it = track.find(h[ch]);
				track.erase(it);
			}
			if(left==nch-1)
			{	if(track.size()==1)
				{	it = track.begin();
					sdfs(ch,index,1,it->first+1);
				}
				else if(track.size()==0)
				{	sdfs(ch,index,1,1);
				}
				else if(track.size()>1)
				{	sdfs(ch,index,0,-1);
				}
			}
			else
			{	sdfs(ch,index,0,-1);
			}
			track[h[ch]]++;
		}
	}
	track.clear();
}

int totald()
{

	g_ans=0;
	g_add=10000000;
	for(int i=1;i<=n;i++)mark[i]=0;
	dfs(1);
	for(int i=1;i<=n;i++)mark[i]=0;
	sdfs(1,-1,0,-1);
	if(g_ans==0)cout<<-1<<endl;
	else if(g_ans==1)cout<<g_add<<endl;

}

int main()
{	int x,y;
	cin>>n;
	for(int i=0;i<n-1;i++)
	{	cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}
	totald();
}


#include<bits/stdc++.h>
using namespace std;

#define maxs 500000
#define pi pair<int,int>
#define mp make_pair
#define pb push_back

vector< vector<int> > graph(maxs);
int mark[maxs]={0};
int sys[maxs]={0};
int h[maxs]={0};

int n;
int g_ans,g_add;

void dfs(int index)
{	mark[index]=1;
	map<int,int> mymap1;
	mymap1.clear();
	map<int,int>::iterator it;
	int eq=1;
	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(mark[ch]==0)
		{	dfs(ch);
			if(sys[ch]==0)eq=0;
			if(sys[ch]==1)mymap1[h[ch]]++;
		}
	}
	if(eq==0)
	{
		sys[index]=0;
		h[index]=-1;
	}
	else if(eq==1)
	{	if(mymap1.size()>1)
		{	sys[index]=0;
			h[index]=-1;
		}
		else if(mymap1.size()==1)
		{	sys[index]=1;
			it = mymap1.begin();
			h[index] = it->first+1;
		}
		else if(mymap1.size()==0)
		{	sys[index]=1;
			h[index]=1;
		}
	}
	mymap1.clear();
}

void sdfs(int index,int p,int st,int hei)
{
	mark[index]=1;

	map<int,int> track;
	int nch = graph[index].size();
	int add=0;

	map<int,int>::iterator it;

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	add+=sys[ch];
			track[h[ch]]++;
		}
		else
		{	add+=st;
			track[hei]++;
		}
	}

	if(add==nch)
	{	int total=0;
		if(track.size()==2)
		{	g_ans=1;
			it=track.begin();
			total += it->first;
			it++;
			total += it->first;
			if(total%2==0)total=1;
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==1)
		{	g_ans=1;
			it=track.begin();
			total = it->first;
			if(total%2==0)total=1;
			if(g_add>total)
			{	g_add=total;
			}
		}
		else if(track.size()==0)
		{	g_ans=1;
			total = 0;
			if(g_add>total)
			{	g_add=total;
			}
		}
	}

	for(int i=0;i<graph[index].size();i++)
	{	int ch = graph[index][i];
		if(ch!=p)
		{	int left = add - sys[ch];
			track[h[ch]]--;
			if(track[h[ch]]==0)
			{	it = track.find(h[ch]);
				track.erase(it);
			}
			if(left==nch-1)
			{	if(track.size()==1)
				{	it = track.begin();
					sdfs(ch,index,1,it->first+1);
				}
				else if(track.size()==0)
				{	sdfs(ch,index,1,1);
				}
				else if(track.size()>1)
				{	sdfs(ch,index,0,-1);
				}
			}
			else
			{	sdfs(ch,index,0,-1);
			}
			track[h[ch]]++;
		}
	}
	track.clear();
}

int totald()
{

	g_ans=0;
	g_add=10000000;
	for(int i=1;i<=n;i++)mark[i]=0;
	dfs(1);
	for(int i=1;i<=n;i++)mark[i]=0;
	sdfs(1,-1,0,-1);
	if(g_ans==0)cout<<-1<<endl;
	else if(g_ans==1)
	{	cout<<g_add<<endl;
	}

}

int main()
{	int x,y;
	cin>>n;
	for(int i=0;i<n-1;i++)
	{	cin>>x>>y;
		graph[x].pb(y);
		graph[y].pb(x);
	}
	totald();
}


#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define ll long long
#define ld long double
#define sz(a) ((int)(a).size())
#define clr(a,v) memset(a, v, sizeof(a))
#define all(a) (a).begin(),(a).end()
#define pii pair<int,int>
#define EPS 1e-9
#define debug cout<<"!!?!!\n"
#define ler freopen("inspection.in","r",stdin);freopen("inspection.out","w",stdout)
#define fastio ios::sync_with_stdio(0), cin.tie(0)
using namespace std;
#define MOD 1000000009
#define INF 1000000000
#define MAXN 500105

vector <int> adj[MAXN];
int len[MAXN], junta[MAXN], ans= INF;

#define IF if(x==2)
void dfs(int x, int pai, int lenUp){
	map <int,int> mapa;
	if(lenUp!=0) mapa[lenUp]++;

	int cnt=0, idx;
	for(int i=0, y; i<sz(adj[x]); i++){
		y= adj[x][i]; if(y==pai) continue;
		mapa[len[y]]++;
		if(!junta[y]){
			cnt++;
			idx=y;
		}
	}

	if(cnt>1) return;
	if(sz(mapa)>2) return;

	if(sz(mapa)==1 && cnt==0){
		ans= min(ans, mapa.begin()->F);
		//cout << "idx " << x << "  ans="<< mapa.begin()->F << endl;
	}
	else if(sz(mapa)==2 && cnt==0){
		ans= min(ans, mapa.begin()->F + ((++mapa.begin())->F));
		//cout << mapa.begin()->F <<"+"<< ((++mapa.begin())->F) << endl;
	}
	if(cnt==1){
		int a, b;
		a= mapa.begin()->F;
		if(sz(mapa)==1){
			if(mapa[a]==1) dfs(idx,x,lenUp+1);
			else dfs(idx,x,a+1);
		}
		else{
			b= (++mapa.begin())->F;
			if(len[idx]!=a) swap(a,b);
			if(mapa[a]==1) dfs(idx,x,b+1);
		}
	}
	else{
		int a, b;
		a= mapa.begin()->F;
		for(int i=0, y; i<sz(adj[x]); i++){
			y=adj[x][i]; if(y==pai) continue;
			if(sz(mapa)==1){
				if(mapa[a]==1) dfs(y,x,lenUp+1);
				else dfs(y,x,a+1);
			}
			else{
				b= (++mapa.begin())->F;
				if(len[y]!=a) swap(a,b);
				if(mapa[a]==1) dfs(y,x,b+1);
			}
		}
	}
}

void init(int x, int pai){
	junta[x]=1;
	if(sz(adj[x])==1 && x!=pai) len[x]=1;
	for(int i=0, y; i<sz(adj[x]); i++){
		y=adj[x][i]; if(y==pai) continue;
		init(y,x);
		if(len[x]!=-1 && len[y]+1!=len[x]) junta[x]=0;
		len[x]= max(len[x],len[y]+1);
		if(!junta[y]) junta[x]=0;
	}
}

int main(){
	int n;
	cin >> n;

	for(int i=1, x, y; i<n; i++){
		cin >> x >> y;
		x--; y--;
		adj[x].pb(y);
		adj[y].pb(x);
	}
	clr(len,-1);
	init(0,0);
	//for(int i=0; i<n; i++) cout << junta[i] << " "; cout << endl;
	dfs(0,0,0);

	while(ans%2==0 && ans!=INF){ ans/=2;}
	if(ans==INF) cout << -1 << endl;
	else cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;

int n,d,tme[1001],s;
string ans[1001][3],str1[1001],str2[1001];

int main(){
	cin >> n >> d ;
	for(int i=1;i<=n;i++)
		cin >> str1[i] >> str2[i] >> tme[i];

	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			if(str1[i] == str2[j] and str2[i] == str1[j] and tme[j] - tme[i] > 0 and tme[j] - tme[i] <= d){

				ans[++s][1] = str1[i];
				ans[s][2] = str2[i];
			}
		}
	}
	int temp = s;
	for(int i=1;i<=s;i++)
		for(int j=i+1;j<=s;j++)
			if(ans[i][1] == ans[j][2]){
				temp--;
				ans[j][2] = "0";
				ans[j][1] = "0";
			}

	cout << temp << endl;

	for(int i=1;i<=s;i++)
		if(ans[i][1] != "0")
		cout << ans[i][1] << ' ' << ans[i][2]<<endl;
}


#include<bits/stdc++.h>
using namespace std;

int n,d,tme[1001],s;
string ans[1001][3],str1[1001],str2[1001];

int main(){
	cin >> n >> d ;
	for(int i=1;i<=n;i++)
		cin >> str1[i] >> str2[i] >> tme[i];

	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			if(str1[i] == str2[j] and str2[i] == str1[j] and tme[j] - tme[i] > 0 and tme[j] - tme[i] <= d){

				ans[++s][1] = str1[i];
				ans[s][2] = str2[i];
			}
		}
	}
/*	int temp = s;
	for(int i=1;i<=s;i++)
		for(int j=i+1;j<=s;j++)
			if(ans[i][1] == ans[j][2] or ans[i][1] == ans[j][1] and i!=j){
				temp--;
				ans[j][2] = "0";
				ans[j][1] = "0";
			}

	cout << temp << endl;
*/
	cout << s << endl;

	for(int i=1;i<=s;i++)
		cout << ans[i][1] << ' ' << ans[i][2]<<endl;
}


#include<bits/stdc++.h>
using namespace std;

int n,d,tme[1001],s;
string ans[1001][3],str1[1001],str2[1001];

int main(){
	cin >> n >> d ;
	for(int i=1;i<=n;i++)
		cin >> str1[i] >> str2[i] >> tme[i];

	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			if(str1[i] == str2[j] and str2[i] == str1[j] and tme[j] - tme[i] > 0 and tme[j] - tme[i] <= d){

				ans[++s][1] = str1[i];
				ans[s][2] = str2[i];
			}
		}
	}
	cout << s << endl;
	for(int i=1;i<=s;i++)
		cout << ans[i][1] << ' ' << ans[i][2]<<endl;
}


#include<bits/stdc++.h>
using namespace std;

int n,d,tme[10002],s;
string ans[2002][3],A[2002],B[2002];

set<pair<string,string > > ss;

int main(){
	cin >> n >> d ;
	for(int i=1;i<=n;i++)
		cin >> A[i] >> B[i] >> tme[i];

	for(int i=1;i<=n;i++)
		for(int j=i+1;j<=n;j++){
			if(tme[j] - tme[i] > d)
				break;

			if(tme[j] - tme[i] > 0 && A[i] == B[j] && A[j] == B[i])
				ss.insert(make_pair(max(A[i],B[j]),min(A[j],B[i])));

			}
	cout << ss.size() << endl;

	for(set < pair < string,string > > ::iterator p =ss.begin(); p!=ss.end();++p)
		cout << p->first << ' ' << p->second << endl;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 2005
#define H 10007
#define FI(a, b, c) for(int a = b; a <= c; a++)
#define FD(a, b, c) for(int a = b; a >= c; a--)
using namespace std;

int n, d, l[N][N], p, t, A, X[H];
char a[22], b[22], c[N][22], h[H][22];
bool f[N][N];

int s(char* c){
	int s = 0;
	for(int i = 0; c[i]; i++) s = (s + (i + 7) * c[i]) % H;
	while(X[s] && strcmp(c, h[s])) s = s++ % H;
	return s;
}

int main(){
	FI(i, 1, N - 1) FI(j, 1, N - 1) l[i][j] = -1e9;
	scanf("%d %d", &n, &d);

	while(n--){
		scanf("%s %s %d", a, b, &t);
		int x = s(a), y = s(b);
		if(!X[x]){
			strcpy(h[x], a);
			strcpy(c[++p], a);
			X[x] = p;
		}
		if(!X[y]){
			strcpy(h[y], b);
			strcpy(c[++p], b);
			X[y] = p;
		}
		x = X[x], y = X[y];
		if(t - l[y][x] <= d && t > l[y][x]) f[x][y] = f[y][x] = 1;
		l[x][y] = t;
	}

	FI(i, 1, p) FI(j, i + 1, p) A += f[i][j];
	printf("%d\n", A);
	FI(i, 1, p) FI(j, i + 1, p) if(f[i][j])
		printf("%s %s\n", c[i], c[j]);
	scanf("\n");
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 1005
#define H 10007
#define FI(a, b, c) for(int a = b; a <= c; a++)
#define FD(a, b, c) for(int a = b; a >= c; a--)
using namespace std;

int n, d, l[N][N], p, t, A, X[H];
char a[22], b[22], c[N][22], h[H][22];
bool f[N][N];

int s(char* c){
	int s = 0;
	for(int i = 0; c[i]; i++) s = (s + (i + 7) * c[i]) % H;
	while(X[s] && strcmp(c, h[s])) s = s++ % H;
	return s;
}

int main(){
	scanf("%d %d", &n, &d);
	FI(i, 1, n) FI(j, 1, n) l[i][j] = -1e9;

	while(n--){
		scanf("%s %s %d", a, b, &t);
		int x = s(a), y = s(b);
		if(!X[x]){
			strcpy(h[x], a);
			strcpy(c[++p], a);
			X[x] = p;
		}
		if(!X[y]){
			strcpy(h[y], b);
			strcpy(c[++p], b);
			X[y] = p;
		}
		x = X[x], y = X[y];
		if(t - l[y][x] <= d) f[x][y] = f[y][x] = 1;
		l[x][y] = t;
	}

	FI(i, 1, p) FI(j, i, p) A += f[i][j];
	printf("%d\n", A);
	FI(i, 1, p) FI(j, i, p) if(f[i][j])
		printf("%s %s\n", c[i], c[j]);
	scanf("\n");
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 2005
#define H 10007
#define FI(a, b, c) for(int a = b; a <= c; a++)
#define FD(a, b, c) for(int a = b; a >= c; a--)
using namespace std;

int n, d, l[N][N], p, t, A, X[H];
char a[22], b[22], c[N][22], h[H][22];
bool f[N][N];

int s(char* c){
	int s = 0;
	for(int i = 0; c[i]; i++) s = (s + (i + 7) * c[i]) % H;
	while(X[s] && strcmp(c, h[s])) s = s++ % H;
	return s;
}

int main(){
	scanf("%d %d", &n, &d);
	FI(i, 1, N - 1) FI(j, 1, N - 1) l[i][j] = -1e9;

	while(n--){
		scanf("%s %s %d", a, b, &t);
		int x = s(a), y = s(b);
		if(!X[x]){
			strcpy(h[x], a);
			strcpy(c[++p], a);
			X[x] = p;
		}
		if(!X[y]){
			strcpy(h[y], b);
			strcpy(c[++p], b);
			X[y] = p;
		}
		x = X[x], y = X[y];
		if(t - l[y][x] <= d) f[x][y] = f[y][x] = 1;
		l[x][y] = t;
	}

	FI(i, 1, p) FI(j, i, p) A += f[i][j];
	printf("%d\n", A);
	FI(i, 1, p) FI(j, i, p) if(f[i][j])
		printf("%s %s\n", c[i], c[j]);
	scanf("\n");
}


#include <cstdio>
#include <string>
#include <iostream>
#define N 1005
#define FI(a, b, c) for(int a = b; a <= c; a++)
using namespace std;

int n, d, P, t[N];
string a[N], b[N], C[N], D[N];
bool f[N][N];

int main(){
	scanf("%d %d", &n, &d);
	FI(i, 1, n){
		cin >> a[i] >> b[i] >> t[i];
		FI(j, 1, i - 1) if(a[i] == b[j] && a[j] == b[i] && t[i] > t[j] && t[i] - t[j] <= d && !f[i][j]){
			f[i][j] = f[j][i] = 1;
			C[P] = a[i];
			D[P++] = b[i];
		}
	}
	printf("%d\n", P);
	FI(i, 0, P - 1) cout << C[i] << ' ' << D[i] << '\n';
	scanf("\n");
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#define N 2005
#define H 10007
#define FI(a, b, c) for(int a = b; a <= c; a++)
#define FD(a, b, c) for(int a = b; a >= c; a--)
using namespace std;

int n, d, l[N][N], p, t, A, X[H];
char a[22], b[22], c[N][22], h[H][22];
bool f[N][N];

int s(char* c){
	int s = 0;
	for(int i = 0; c[i]; i++) s = (s + (i + 7) * c[i]) % H;
	while(X[s] && strcmp(c, h[s])) s = ++s % H;
	return s;
}

int main(){
	FI(i, 1, N - 1) FI(j, 1, N - 1) l[i][j] = -1e9;
	scanf("%d %d", &n, &d);

	while(n--){
		scanf("%s %s %d", a, b, &t);
		int x = s(a), y = s(b);
		if(!X[x]){
			strcpy(h[x], a);
			strcpy(c[++p], a);
			X[x] = p;
		}
		if(!X[y]){
			strcpy(h[y], b);
			strcpy(c[++p], b);
			X[y] = p;
		}
		x = X[x], y = X[y];
		if(t - l[y][x] <= d && t > l[y][x]) f[x][y] = f[y][x] = 1;
		l[x][y] = t;
	}

	FI(i, 1, p) FI(j, i + 1, p) A += f[i][j];
	printf("%d\n", A);
	FI(i, 1, p) FI(j, i + 1, p) if(f[i][j])
		printf("%s %s\n", c[i], c[j]);
	scanf("\n");
}


#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#define N 2005
#define H 10007
#define FI(a, b, c) for(int a = b; a <= c; a++)
#define FD(a, b, c) for(int a = b; a >= c; a--)
using namespace std;

int n, d, l[N][N], p, t, A;
char a[22], b[22], c[N][22];
bool f[N][N];
map<string, int> m;

int main(){
	FI(i, 1, N - 1) FI(j, 1, N - 1) l[i][j] = -1e9;
	scanf("%d %d", &n, &d);

	while(n--){
		scanf("%s %s %d", a, b, &t);
		if(!m[a]){
			strcpy(c[++p], a);
			m[a] = p;
		}
		if(!m[b]){
			strcpy(c[++p], b);
			m[b] = p;
		}
		int x = m[a], y = m[b];
		if(t - l[y][x] <= d && t > l[y][x]) f[x][y] = f[y][x] = 1;
		l[x][y] = t;
	}

	FI(i, 1, p) FI(j, i + 1, p) A += f[i][j];
	printf("%d\n", A);
	FI(i, 1, p) FI(j, i + 1, p) if(f[i][j])
		printf("%s %s\n", c[i], c[j]);
	scanf("\n");
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#define N 2005
#define H 10007
#define FI(a, b, c) for(int a = b; a <= c; a++)
#define FD(a, b, c) for(int a = b; a >= c; a--)
using namespace std;

int n, d, l[N][N], p, t, A;
char a[22], b[22], c[N][22];
bool f[N][N];
map<string, int> m;

int main(){
	FI(i, 1, N - 1) FI(j, 1, N - 1) l[i][j] = -1e9;
	scanf("%d %d", &n, &d);

	while(n--){
		scanf("%s %s %d", a, b, &t);
		if(!m[a]){
			strcpy(c[++p], a);
			m[a] = p;
		}
		if(!m[b]){
			strcpy(c[++p], b);
			m[b] = p;
		}
		int x = m[a], y = m[b];
		if(t - l[y][x] <= d && t > l[y][x]) f[x][y] = f[y][x] = 1;
		l[x][y] = t;
	}

	FI(i, 1, p) FI(j, i + 1, p) A += f[i][j];
	printf("%d\n", A);
	FI(i, 1, p) FI(j, i + 1, p) if(f[i][j])
		printf("%s %s\n", c[i], c[j]);
	scanf("\n");
}


#include <cstdio>
#include <string>
#include <iostream>
#define N 1005
#define FI(a, b, c) for(int a = b; a <= c; a++)
using namespace std;

int n, d, P, t[N];
string a[N], b[N], C[N], D[N];
bool f[N][N];

int main(){
	scanf("%d %d", &n, &d);
	FI(i, 1, n){
		cin >> a[i] >> b[i] >> t[i];
		FI(j, 1, i - 1) if(a[i] == a[j] && b[i] == b[j] || a[i] == b[j] && a[j] == b[i] && t[i] > t[j] && t[i] - t[j] <= d && !f[i][j]){
			f[i][j] = f[j][i] = 1;
			C[P] = a[i];
			D[P++] = b[i];
		}
	}
	printf("%d\n", P);
	FI(i, 0, P - 1) cout << C[i] << ' ' << D[i] << '\n';
	scanf("\n");
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#define N 2005
#define FI(a, b, c) for(int a = b; a <= c; a++)
#define FD(a, b, c) for(int a = b; a >= c; a--)
using namespace std;

int n, d, l[N][N], p, t, A, P;
char a[22], b[22], c[N][22], o[N][22], O[N][22];
bool f[N][N];
map<string, int> m;

int main(){
	FI(i, 1, N - 1) FI(j, 1, N - 1) l[i][j] = -1e9;
	scanf("%d %d", &n, &d);

	while(n--){
		scanf("%s %s %d", a, b, &t);
		if(!m[a]){
			strcpy(c[++p], a);
			m[a] = p;
		}
		if(!m[b]){
			strcpy(c[++p], b);
			m[b] = p;
		}
		int x = m[a], y = m[b];
		if(t - l[y][x] <= d && t > l[y][x] && !f[x][y]){
			f[x][y] = f[y][x] = 1;
			strcpy(o[P], a);
			strcpy(O[P++], b);
		}
		l[x][y] = t;
	}

	printf("%d\n", P);
	FI(i, 0, P - 1)
		printf("%s %s\n", o[i], O[i]);
	scanf("\n");
}


#include <cstdio>
#include <string>
#include <iostream>
#include <map>
#define N 1005
#define FI(a, b, c) for(int a = b; a <= c; a++)
using namespace std;

int n, d, P, t[N];
string a[N], b[N], C[N], D[N];
bool f[N][N];
map<string, map<string, bool> > m;

int main(){
	scanf("%d %d", &n, &d);
	FI(i, 1, n){
		cin >> a[i] >> b[i] >> t[i];
		FI(j, 1, i - 1) if(a[i] == b[j] && a[j] == b[i] && t[i] > t[j] && t[i] - t[j] <= d && !m[a[i]][b[i]]){
			m[a[i]][b[i]] = m[b[i]][a[i]] = 1;
			C[P] = a[i];
			D[P++] = b[i];
		}
	}
	printf("%d\n", P);
	FI(i, 0, P - 1) cout << C[i] << ' ' << D[i] << '\n';
	scanf("\n");
}


#include <iostream>
#include <string.h>
using namespace std;
string a1[1001],b1[1001];
bool prov(string &a,string& b,int n,int t)
{
    bool h=true;
    int j;
    for(j=0;j<t;j++)
    if((strcmp(a1[j].c_str(),a.c_str())==0 && strcmp(b1[j].c_str(),b.c_str())==0)||(strcmp(a1[j].c_str(),b.c_str())==0 && strcmp(b1[j].c_str(),a.c_str())==0)) {h=false;break;}
    return h;
}
int main()
{
string a[1001],b[1001];
int i,j,n,d,c[1001],t=0;
cin>>n>>d;
for(i=0;i<n;i++)
cin>>a[i]>>b[i]>>c[i];
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(strcmp(a[i].c_str(),b[j].c_str())==0 && strcmp(b[i].c_str(),a[j].c_str())==0 && c[j]-c[i]>0 && c[j]-c[i]<=d && strcmp(a[i].c_str(),b[i].c_str())!=0 && strcmp(a[j].c_str(),b[j].c_str())!=0 && prov(a[i],b[i],n,t)==true) {a1[t]=a[i];b1[t]=b[i];t++;}
cout<<t<<"\n";
for(i=0;i<t;i++)
cout<<a1[i]<<" "<<b1[i]<<"\n";
return 0;
}


#include <iostream>
#include <string.h>
using namespace std;
string a1[1001],b1[1001];
bool prov(string &a,string& b,int n,int t)
{
    bool h=true;
    int j;
    for(j=0;j<t;j++)
    if((strcmp(a1[j].c_str(),a.c_str())==0&&strcmp(b1[j].c_str(),b.c_str())==0)||(strcmp(a1[j].c_str(),b.c_str())==0&&strcmp(b1[j].c_str(),a.c_str())==0)) {h=false;break;}
    return h;
}
int main()
{
string a[1001],b[1001],;
int i,j,n,d,c[1001],c1,t=0,k=0;
cin>>n>>d;
for(i=0;i<n;i++)
cin>>a[i]>>b[i]>>c[i];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(a[i].c_str(),b[j].c_str())==0&&strcmp(b[i].c_str(),a[j].c_str())==0&&c[j]-c[i]>0&&c[j]-c[i]<=d&&strcmp(a[i].c_str(),b[i].c_str())!=0&&strcmp(a[j].c_str(),b[j].c_str())!=0) {if(prov(a[i],b[i],n,t)){a1[t]=a[i];b1[t]=b[i];t++;}}
}
}
cout<<t<<endl;
for(i=0;i<t;i++)
cout<<a1[i]<<" "<<b1[i]<<endl;
return 0;
}


#include <iostream>
#include <string.h>
using namespace std;
string a1[1001],b1[1001];
bool prov(string &a,string& b,int n,int t)
{
    bool h=true;
    int j;
    for(j=0;j<t;j++)
    if((strcmp(a1[j].c_str(),a.c_str())==0 && strcmp(b1[j].c_str(),b.c_str())==0)||(strcmp(a1[j].c_str(),b.c_str())==0 && strcmp(b1[j].c_str(),a.c_str())==0)) {h=false;break;}
    return h;
}
int main()
{
string a[1001],b[1001],;
int i,j,n,d,c[1001],t=0;
cin>>n>>d;
for(i=0;i<n;i++)
cin>>a[i]>>b[i]>>c[i];
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(strcmp(a[i].c_str(),b[j].c_str())==0 && strcmp(b[i].c_str(),a[j].c_str())==0 && c[j]-c[i]>0 && c[j]-c[i]<=d && strcmp(a[i].c_str(),b[i].c_str())!=0 && strcmp(a[j].c_str(),b[j].c_str())!=0 && prov(a[i],b[i],n,t)==true) {a1[t]=a[i];b1[t]=b[i];t++;}
cout<<t<<"\n";
for(i=0;i<t;i++)
cout<<a1[i]<<" "<<b1[i]<<"\n";
return 0;
}


#include <iostream>
#include <string.h>
using namespace std;
string a1[1001],b1[1001];
bool prov(string &a,string& b,int n,int t)
{
    bool h=true;
    int j;
    for(j=0;j<t;j++)
    if((strcmp(a1[j].c_str(),a.c_str())==0&&strcmp(b1[j].c_str(),b.c_str())==0)||(strcmp(a1[j].c_str(),b.c_str())==0&&strcmp(b1[j].c_str(),a.c_str())==0)) {h=false;break;}
    return h;
}
int main()
{
string a[1001],b[1001],;
int i,j,n,d,c[1001],t=0;
cin>>n>>d;
for(i=0;i<n;i++)
cin>>a[i]>>b[i]>>c[i];
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(strcmp(a[i].c_str(),b[j].c_str())==0&&strcmp(b[i].c_str(),a[j].c_str())==0&&c[j]-c[i]>0&&c[j]-c[i]<=d&&strcmp(a[i].c_str(),b[i].c_str())!=0&&strcmp(a[j].c_str(),b[j].c_str())!=0&&prov(a[i],b[i],n,t)) {a1[t]=a[i];b1[t]=b[i];t++;}
cout<<t<<endl;
for(i=0;i<t;i++)
cout<<a1[i]<<" "<<b1[i]<<endl;
}


#include <iostream>
#include <string.h>
using namespace std;
string a1[1001],b1[1001];
bool prov(string &a,string& b,int n,int t)
{
    bool h=true;
    int j;
    for(j=0;j<t;j++)
    if((strcmp(a1[j].c_str(),a.c_str())==0 && strcmp(b1[j].c_str(),b.c_str())==0)||(strcmp(a1[j].c_str(),b.c_str())==0 && strcmp(b1[j].c_str(),a.c_str())==0)) {h=false;break;}
    return h;
}
int main()
{
string a[1001],b[1001],;
int i,j,n,d,c[1001],t=0;
cin>>n>>d;
for(i=0;i<n;i++)
cin>>a[i]>>b[i]>>c[i];
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(strcmp(a[i].c_str(),b[j].c_str())==0 && strcmp(b[i].c_str(),a[j].c_str())==0 && c[j]-c[i]>0 && c[j]-c[i]<=d && strcmp(a[i].c_str(),b[i].c_str())!=0 && strcmp(a[j].c_str(),b[j].c_str())!=0 && prov(a[i],b[i],n,t)==true) {a1[t]=a[i];b1[t]=b[i];t++;}
cout<<t<<endl;
for(i=0;i<t;i++)
cout<<a1[i]<<" "<<b1[i]<<endl;
return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int MAXN = 1010;

string f1[MAXN], f2[MAXN];
int st[MAXN];
int n, d;
set< pair<string,string> > fpairs;

int main()
{
    cin >> n >> d;
    for (int i = 0; i < n; i++)
        cin >> f1[i] >> f2[i] >> st[i];
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (st[j] - st[i] <= d && st[j] - st[i] > 0)
                if (f1[i] == f2[j] && f2[i] == f1[j])
                    fpairs.insert(make_pair(f1[i], f2[i]));
    cout << fpairs.size() << endl;
    while (fpairs.size())
    {
        pair<string,string> cfp = *(fpairs.begin());
        cout << cfp.first << " " << cfp.second << endl;
        fpairs.erase(make_pair(cfp.first, cfp.second));
        fpairs.erase(make_pair(cfp.second, cfp.first));
    }
    //system("pause");
    return 0;
}


#include <iostream>
#include <string>
#include <utility>

using namespace std;

typedef pair<int,int> pint;

const int MAXN = 1010;

string names[MAXN];
int mes[MAXN][MAXN];
bool mark[MAXN];
int n, d;
pint fr[MAXN];
int lfr;

int abs(int inp)
{
    if (inp >= 0)
        return inp;
    return -1 * inp;
}

void addp(pint inp)
{
    //cout << "addp" << endl;
    bool dup = false;
    for (int i = 0; i < lfr; i++)
    {
        if (inp.first == fr[i].first)
        {
            if (inp.second == fr[i].second)
                dup = true;
        }
        else if (inp.second == fr[i].first)
        {
            if (inp.first == fr[i].second)
                dup = true;
        }
    }
    if (!dup)
    {
        fr[lfr] = inp;
        lfr++;
    }
    //else
        //cout << "dup: " << names[inp.first] << ", " << names[inp.second] << endl;
}

int add2dic(string name)
{
    int c;
    bool d = true;
    for (int i = 0; names[i] != ""; i++)
    {
        if (names[i] == name)
        {
            d = false;
            break;
        }
        c++;
    }
    if (d)
        names[c] = name;
    return c;
}

int min(int a, int b)
{
    if (a == 0)
        return b;
    if (b == 0)
        return a;
    if (a > b)
        return b;
    return a;
}

void dfs(int v)
{
    mark[v] = true;
    //cout << names[v] << " marked" << endl;
    int buf;
    pint pbuf;
    for (int i = 0; i < n; i++)
        if ( (mes[i][v] > 0) && (!mark[i]) )
        {
            dfs(i);
            buf = abs(mes[i][v] - mes[v][i]);
            if (buf <= d)
            {
                pbuf.first = v;
                pbuf.second = i;
                addp(pbuf);
            }
        }
}

/**/
int main()
{
    // Hashing validated
    // Graph creation validate
    cin >> n >> d;
    string buf1, buf2;
    int ibuf;
    int cbuf1, cbuf2;
    int cdic = 0;
    int buf, lbuf;
    for (int i = 0; i < n; i++)
    {
        cin >> buf1 >> buf2 >> ibuf;
        cbuf1 = add2dic(buf1);
        cbuf2 = add2dic(buf2);
        mes[cbuf1][cbuf2] = min(mes[cbuf1][cbuf2], ibuf);
        buf = abs(mes[cbuf2][cbuf1] - ibuf);
        lbuf = abs(mes[cbuf2][cbuf1] - mes[cbuf1][cbuf2]);
        if (buf < lbuf)
        {
            mes[cbuf1][cbuf2] = ibuf;
        }
        ///
        if (cbuf2 > cdic)
            cdic = cbuf2;
        //mes[cbuf2][cbuf1] = min(mes[cbuf2][cbuf1], ibuf);
    }
    dfs(0);
    /**
    for (int i = 0; i <= cdic; i++)
        cout << i << ": " << names[i] << endl;
    for (int i = 0; i < cdic + 1; i++)
    {
        for (int j = 0; j < cdic + 1; j++)
        {
            if (mes[i][j] > 9)
                cout << mes[i][j] << "  ";
            else
                cout << " " << mes[i][j] << "  ";
        }
        cout << endl;
    }
    /**/
    cout << lfr << endl;
    for (int i = 0; i < lfr; i++)
    {
        cout << names[fr[i].first] << " " << names[fr[i].second] << endl;
    }
    //system("pause");
    return 0;
}
/**
int main()
{
    int a, b;
    cin >> a >> b;
    cout << min(a, b) << endl;
    system("pause");
    return 0;
}
/**/


#include <iostream>
#include <string>
#include <utility>

using namespace std;

typedef pair<int,int> pint;

const int MAXN = 1010;

string names[MAXN];
int mes[MAXN][MAXN];
bool mark[MAXN];
int n, d;
pint fr[MAXN];
int lfr;

void addp(pint inp)
{
    //cout << "addp" << endl;
    bool dup = false;
    for (int i = 0; i < lfr; i++)
    {
        if (inp.first == fr[i].first)
        {
            if (inp.second == fr[i].second)
                dup = true;
        }
        else if (inp.second == fr[i].first)
        {
            if (inp.first == fr[i].second)
                dup = true;
        }
    }
    if (!dup)
    {
        fr[lfr] = inp;
        lfr++;
    }
    //else
        //cout << "dup: " << names[inp.first] << ", " << names[inp.second] << endl;
}

int add2dic(string name)
{
    int c;
    bool d = true;
    for (int i = 0; names[i] != ""; i++)
    {
        if (names[i] == name)
        {
            d = false;
            break;
        }
        c++;
    }
    if (d)
        names[c] = name;
    return c;
}

int min(int a, int b)
{
    if (a == 0)
        return b;
    if (b == 0)
        return a;
    if (a > b)
        return b;
    return a;
}

void dfs(int v)
{
    mark[v] = true;
    int buf;
    pint pbuf;
    for (int i = 0; i < n; i++)
        if ( (mes[i][v] > 0) && (!mark[i]) )
        {
            dfs(i);
            buf = abs(mes[i][v] - mes[v][i]);
            if (buf <= d)
            {
                pbuf.first = v;
                pbuf.second = i;
                addp(pbuf);
            }
        }
}

/**/
int main()
{
    // Hashing validated
    // Graph creation validate
    cin >> n >> d;
    string buf1, buf2;
    int ibuf;
    int cbuf1, cbuf2;
    int cdic = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> buf1 >> buf2 >> ibuf;
        cbuf1 = add2dic(buf1);
        cbuf2 = add2dic(buf2);
        mes[cbuf1][cbuf2] = min(mes[cbuf1][cbuf2], ibuf);
        ///
        if (cbuf2 > cdic)
            cdic = cbuf2;
        //mes[cbuf2][cbuf1] = min(mes[cbuf2][cbuf1], ibuf);
    }
    dfs(0);
    /**
    for (int i = 0; i < cdic + 1; i++)
    {
        for (int j = 0; j < cdic + 1; j++)
            cout << mes[i][j] << " ";
        cout << endl;
    }
    /**/
    cout << lfr << endl;
    for (int i = 0; i < lfr; i++)
    {
        cout << names[fr[i].first] << " " << names[fr[i].second] << endl;
    }
    //system("pause");
    return 0;
}
/**
int main()
{
    int a, b;
    cin >> a >> b;
    cout << min(a, b) << endl;
    system("pause");
    return 0;
}
/**/


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

const int MAXN = 1010;

string f1[MAXN], f2[MAXN];
int st[MAXN];
int n, d;
vector< pair<string,string> > fpairs;

int main()
{
    cin >> n >> d;
    for (int i = 0; i < n; i++)
        cin >> f1[i] >> f2[i] >> st[i];
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (st[j] - st[i] <= d && st[j] - st[i] > 0)
                if (f1[i] == f2[j] && f2[i] == f1[j])
                    fpairs.push_back(make_pair(f1[i], f2[i]));
    cout << fpairs.size() << endl;
    for (int i = 0; i < fpairs.size(); i++)
        cout << fpairs[i].first << " " << fpairs[i].second << endl;
    //system("pause");
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

const int MAXN = 1010;

string f1[MAXN], f2[MAXN];
int st[MAXN];
int n, d;
vector< pair<string,string> > fpairs;

int main()
{
    cin >> n >> d;
    for (int i = 0; i < n; i++)
        cin >> f1[i] >> f2[i] >> st[i];
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (st[j] - st[i] <= d)
                if (f1[i] == f2[j] && f2[i] == f1[j])
                    fpairs.push_back(make_pair(f1[i], f2[i]));
    cout << fpairs.size() << endl;
    for (int i = 0; i < fpairs.size(); i++)
        cout << fpairs[i].first << " " << fpairs[i].second << endl;
    //system("pause");
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int MAXN = 1010;

string f1[MAXN], f2[MAXN];
int st[MAXN];
int n, d;
set< pair<string,string> > fpairs;
vector<string> ans;

int main()
{
    cin >> n >> d;
    for (int i = 0; i < n; i++)
        cin >> f1[i] >> f2[i] >> st[i];
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (st[j] - st[i] <= d && st[j] - st[i] > 0)
                if (f1[i] == f2[j] && f2[i] == f1[j])
                    fpairs.insert(make_pair(f1[i], f2[i]));
    while (fpairs.size())
    {
        pair<string,string> cfp = *(fpairs.begin());
        ans.push_back(cfp.first + " " + cfp.second);
        fpairs.erase(make_pair(cfp.first, cfp.second));
        fpairs.erase(make_pair(cfp.second, cfp.first));
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++)
        cout << ans[i] << endl;
    //system("pause");
    return 0;
}


#include <iostream>
#include <string>
#include <utility>
#include <cmath>

using namespace std;

typedef pair<int,int> pint;

const int MAXN = 1010;

string names[MAXN];
int mes[MAXN][MAXN];
bool mark[MAXN];
int n, d;
pint fr[MAXN];
int lfr;

void addp(pint inp)
{
    //cout << "addp" << endl;
    bool dup = false;
    for (int i = 0; i < lfr; i++)
    {
        if (inp.first == fr[i].first)
        {
            if (inp.second == fr[i].second)
                dup = true;
        }
        else if (inp.second == fr[i].first)
        {
            if (inp.first == fr[i].second)
                dup = true;
        }
    }
    if (!dup)
    {
        fr[lfr] = inp;
        lfr++;
    }
    //else
        //cout << "dup: " << names[inp.first] << ", " << names[inp.second] << endl;
}

int add2dic(string name)
{
    int c;
    bool d = true;
    for (int i = 0; names[i] != ""; i++)
    {
        if (names[i] == name)
        {
            d = false;
            break;
        }
        c++;
    }
    if (d)
        names[c] = name;
    return c;
}

int min(int a, int b)
{
    if (a == 0)
        return b;
    if (b == 0)
        return a;
    if (a > b)
        return b;
    return a;
}

void dfs(int v)
{
    mark[v] = true;
    int buf;
    pint pbuf;
    for (int i = 0; i < n; i++)
        if ( (mes[i][v] > 0) && (!mark[i]) )
        {
            dfs(i);
            buf = abs(mes[i][v] - mes[v][i]);
            if (buf <= d)
            {
                pbuf.first = v;
                pbuf.second = i;
                addp(pbuf);
            }
        }
}

/**/
int main()
{
    // Hashing validated
    // Graph creation validate
    cin >> n >> d;
    string buf1, buf2;
    int ibuf;
    int cbuf1, cbuf2;
    int cdic = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> buf1 >> buf2 >> ibuf;
        cbuf1 = add2dic(buf1);
        cbuf2 = add2dic(buf2);
        mes[cbuf1][cbuf2] = min(mes[cbuf1][cbuf2], ibuf);
        ///
        if (cbuf2 > cdic)
            cdic = cbuf2;
        //mes[cbuf2][cbuf1] = min(mes[cbuf2][cbuf1], ibuf);
    }
    dfs(0);
    /**
    for (int i = 0; i < cdic + 1; i++)
    {
        for (int j = 0; j < cdic + 1; j++)
            cout << mes[i][j] << " ";
        cout << endl;
    }
    /**/
    cout << lfr << endl;
    for (int i = 0; i < lfr; i++)
    {
        cout << names[fr[i].first] << " " << names[fr[i].second] << endl;
    }
    //system("pause");
    return 0;
}
/**
int main()
{
    int a, b;
    cin >> a >> b;
    cout << min(a, b) << endl;
    system("pause");
    return 0;
}
/**/


#include <iostream>
#include <string>
#include <utility>

using namespace std;

typedef pair<int,int> pint;

const int MAXN = 1010;

string names[MAXN];
int mes[MAXN][MAXN];
bool mark[MAXN];
int n, d;
pint fr[MAXN];
int lfr;

int abs(int inp)
{
    if (inp >= 0)
        return inp;
    return -1 * inp;
}

void addp(pint inp)
{
    //cout << "addp" << endl;
    bool dup = false;
    for (int i = 0; i < lfr; i++)
    {
        if (inp.first == fr[i].first)
        {
            if (inp.second == fr[i].second)
                dup = true;
        }
        else if (inp.second == fr[i].first)
        {
            if (inp.first == fr[i].second)
                dup = true;
        }
    }
    if (!dup)
    {
        fr[lfr] = inp;
        lfr++;
    }
    //else
        //cout << "dup: " << names[inp.first] << ", " << names[inp.second] << endl;
}

int add2dic(string name)
{
    int c;
    bool d = true;
    for (int i = 0; names[i] != ""; i++)
    {
        if (names[i] == name)
        {
            d = false;
            break;
        }
        c++;
    }
    if (d)
        names[c] = name;
    return c;
}

int min(int a, int b)
{
    if (a == 0)
        return b;
    if (b == 0)
        return a;
    if (a > b)
        return b;
    return a;
}

void dfs(int v)
{
    mark[v] = true;
    int buf;
    pint pbuf;
    for (int i = 0; i < n; i++)
        if ( (mes[i][v] > 0) && (!mark[i]) )
        {
            dfs(i);
            buf = abs(mes[i][v] - mes[v][i]);
            if (buf <= d)
            {
                pbuf.first = v;
                pbuf.second = i;
                addp(pbuf);
            }
        }
}

/**/
int main()
{
    // Hashing validated
    // Graph creation validate
    cin >> n >> d;
    string buf1, buf2;
    int ibuf;
    int cbuf1, cbuf2;
    int cdic = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> buf1 >> buf2 >> ibuf;
        cbuf1 = add2dic(buf1);
        cbuf2 = add2dic(buf2);
        mes[cbuf1][cbuf2] = min(mes[cbuf1][cbuf2], ibuf);
        ///
        if (cbuf2 > cdic)
            cdic = cbuf2;
        //mes[cbuf2][cbuf1] = min(mes[cbuf2][cbuf1], ibuf);
    }
    dfs(0);
    /**
    for (int i = 0; i < cdic + 1; i++)
    {
        for (int j = 0; j < cdic + 1; j++)
            cout << mes[i][j] << " ";
        cout << endl;
    }
    /**/
    cout << lfr << endl;
    for (int i = 0; i < lfr; i++)
    {
        cout << names[fr[i].first] << " " << names[fr[i].second] << endl;
    }
    //system("pause");
    return 0;
}
/**
int main()
{
    int a, b;
    cin >> a >> b;
    cout << min(a, b) << endl;
    system("pause");
    return 0;
}
/**/


#include<stdio.h>
#include<string.h>
struct data{
	char A[21];
	char B[21];
	int time;
};
struct data S[1000];
struct data ans[1000];
int ptr;
void printIntoAns(char *A,char *B){
	int i;
	for(i=0;i<ptr;i++)
		if( (!strcmp(A,ans[i].A)&&!strcmp(B,ans[i].B))||(!strcmp(A,ans[i].B)&&!strcmp(B,ans[i].A)) )
			break;
	if( i>=ptr ){
		strcpy(ans[ptr].A,A);
		strcpy(ans[ptr].B,B);
		ptr++;
	}
}
int main(){
	int n,d;
	while( scanf("%d%d",&n,&d)==2 ){
		for(int i=0;i<n;i++)
			scanf("%s%s%d",S[i].A,S[i].B,&S[i].time);
		ptr=0;

		for(int i=0;i<n;i++)
			for(int j=i+1;j<n;j++){
				char *A1=S[i].A;
				char *B1=S[i].B;
				char *A2=S[j].A;
				char *B2=S[j].B;
				if( (!strcmp(A1,B2)&&!strcmp(B1,A2))&&S[j].time-S[i].time<=d&&S[j].time-S[i].time>0 )
					printIntoAns(A1,B1);
			}
		printf("%d\n",ptr);
		for(int i=0;i<ptr;i++)
			printf("%s %s\n",ans[i].A,ans[i].B);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
typedef struct data{
	char name1[30];
	char name2[30];
	int time;
}MAKE;
MAKE M[1000];
bool cmp(MAKE A,MAKE B){
	return A.time<B.time;
}
int main(){
	int n,d;
	while(scanf("%d%d",&n,&d)==2){
		MAKE ANS[1000];
		int ptr=0;
		for(int i=0;i<n;i++)
			scanf("%s%s%d",M[i].name1,M[i].name2,&M[i].time);
		printf("%d\n",M[0].time);
		for(int i=0;i<n;i++)
			for(int j=i+1;j<n&&M[j].time-M[i].time<=d;j++){
				if(strcmp(M[i].name1,M[j].name2)==0&&strcmp(M[i].name2,M[j].name1)==0){
					int k;
					for(k=0;k<ptr;k++)
						if( (strcmp(ANS[k].name1,M[i].name1)==0&&strcmp(ANS[k].name2,M[i].name2)==0)||
							(strcmp(ANS[k].name1,M[i].name2)==0&&strcmp(ANS[k].name2,M[i].name1)==0) )
							break;
					if(k>=ptr){
						strcpy(ANS[ptr].name1,M[i].name1);
						strcpy(ANS[ptr].name2,M[i].name2);
						ptr++;
					}
				}
			}
		printf("%d\n",ptr);
		for(int i=0;i<ptr;i++)
			printf("%s %s\n",ANS[i].name1,ANS[i].name2);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
typedef struct data{
	char name1[30];
	char name2[30];
	int time;
}MAKE;
MAKE M[1000];
bool cmp(MAKE A, MAKE B){
	return A.time<B.time;
}
int main(){
	int n,d;
	while(scanf("%d%d",&n,&d)==2){
		MAKE ANS[1000];
		int ptr=0;
		for(int i=0;i<n;i++)
			scanf("%s%s%d",M[i].name1,M[i].name2,&M[i].time);
		std::sort(M,M+n,cmp);
		for(int i=0;i<n;i++)
			for(int j=i+1;j<n&&M[j].time-M[i].time<=d;j++)
				if( strcmp(M[i].name1,M[j].name2)==0&&strcmp(M[i].name2,M[j].name1)==0 ){
					int k;
					for(k=0;k<ptr;k++)
						if( (strcmp(M[i].name1,ANS[k].name1)==0&&strcmp(M[i].name2,ANS[k].name2)==0) ||
							(strcmp(M[i].name1,ANS[k].name2)==0&&strcmp(M[i].name2,ANS[k].name1)==0) )
							break;
					if(k>=ptr){
						strcpy(ANS[ptr].name1,M[i].name1);
						strcpy(ANS[ptr].name2,M[i].name2);
						ptr++;
					}
				}
		printf("%d\n",ptr);
		for(int i=0;i<ptr;i++)
			printf("%s %s\n",ANS[i].name1,ANS[i].name2);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#define ABS(x) ( (x) >= 0 ? (x) : -(x) )
struct data{
	char A[21];
	char B[21];
	int time;
};
struct data S[1000];
struct data ans[1000];
int ptr;
void printIntoAns(char *A,char *B){
	int i;
	for(i=0;i<ptr;i++)
		if( (!strcmp(A,ans[i].A)&&!strcmp(B,ans[i].B))||(!strcmp(A,ans[i].B)&&!strcmp(B,ans[i].A)) )
			break;
	if( i>=ptr ){
		strcpy(ans[ptr].A,A);
		strcpy(ans[ptr].B,B);
		ptr++;
	}
}
int main(){
	int n,d;
	while( scanf("%d%d",&n,&d)==2 ){
		for(int i=0;i<n;i++)
			scanf("%s%s%d",S[i].A,S[i].B,&S[i].time);
		ptr=0;

		for(int i=0;i<n;i++)
			for(int j=i+1;j<n;j++){
				char *A1=S[i].A;
				char *B1=S[i].B;
				char *A2=S[j].A;
				char *B2=S[j].B;
				if( ((!strcmp(A1,A2)&&!strcmp(B1,B2))||(!strcmp(A1,B2)&&!strcmp(B1,A2)))
					&&ABS(S[j].time-S[i].time)<=d )
					printIntoAns(A1,B1);
			}
		printf("%d\n",ptr);
		for(int i=0;i<ptr;i++)
			printf("%s %s\n",ans[i].A,ans[i].B);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
typedef struct data{
	char name1[30];
	char name2[30];
	int time;
}MAKE;
MAKE M[1000];
bool cmp(MAKE A,MAKE B){
	return A.time<B.time;
}
int main(){
	int n,d;
	while(scanf("%d%d",&n,&d)==2){
		MAKE ANS[1000];
		int ptr=0;
		for(int i=0;i<n;i++)
			scanf("%s%s%d",M[i].name1,M[i].name2,&M[i].time);
		std::sort(M,M+n,cmp);
		for(int i=0;i<n;i++)
			for(int j=i+1;j<n&&M[j].time-M[i].time<=d;j++){
				if(strcmp(M[i].name1,M[j].name2)==0&&strcmp(M[i].name2,M[j].name1)==0){
					int k;
					for(k=0;k<ptr;k++)
						if( (strcmp(ANS[k].name1,M[i].name1)==0&&strcmp(ANS[k].name2,M[i].name2)==0)||
							(strcmp(ANS[k].name1,M[i].name2)==0&&strcmp(ANS[k].name2,M[i].name1)==0) )
							break;
					if(k>=ptr){
						strcpy(ANS[ptr].name1,M[i].name1);
						strcpy(ANS[ptr].name2,M[i].name2);
						ptr++;
					}
				}
			}
		printf("%d\n",ptr);
		for(int i=0;i<ptr;i++)
			printf("%s %s\n",ANS[i].name1,ANS[i].name2);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#define ABS(x) ( (x) >= 0 ? (x) : -(x) )
struct data{
	char A[21];
	char B[21];
	int time;
};
struct data S[1000];
struct data ans[1000];
int ptr;
void printIntoAns(char *A,char *B){
	int i;
	for(i=0;i<ptr;i++)
		if( (!strcmp(A,ans[i].A)&&!strcmp(B,ans[i].B))||(!strcmp(A,ans[i].B)&&!strcmp(B,ans[i].A)) )
			break;
	if( i>=ptr ){
		strcpy(ans[ptr].A,A);
		strcpy(ans[ptr].B,B);
		ptr++;
	}
}
int main(){
	int n,d;
	while( scanf("%d%d",&n,&d)==2 ){
		for(int i=0;i<n;i++)
			scanf("%s%s%d",S[i].A,S[i].B,&S[i].time);
		ptr=0;

		for(int i=0;i<n;i++)
			for(int j=i+1;j<n;j++){
				char *A1=S[i].A;
				char *B1=S[i].B;
				char *A2=S[j].A;
				char *B2=S[j].B;
				if( ((!strcmp(A1,A2)&&!strcmp(B1,B2))||(!strcmp(A1,B2)&&!strcmp(B1,A2)))
					&&ABS(S[j].time-S[i].time)<=d&&ABS(S[j].time-S[i].time)>0 )
					printIntoAns(A1,B1);
			}
		printf("%d\n",ptr);
		for(int i=0;i<ptr;i++)
			printf("%s %s\n",ans[i].A,ans[i].B);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
typedef struct data{
	char name1[30];
	char name2[30];
	int time;
}MAKE;
MAKE M[1000];
bool cmp(MAKE &A, MAKE &B){
	return A.time<B.time;
}
int main(){
	int n,d;
	while(scanf("%d%d",&n,&d)==2){
		MAKE ANS[1000];
		int ptr=0;
		for(int i=0;i<n;i++)
			scanf("%s%s%d",M[i].name1,M[i].name2,&M[i].time);
		std::sort(M,M+n,cmp);
		for(int i=0;i<n;i++)
			for(int j=i+1;j<n&&M[j].time-M[i].time<=d;j++)
				if( strcmp(M[i].name1,M[j].name2)==0&&strcmp(M[i].name2,M[j].name1)==0 ){
					int k;
					for(k=0;k<ptr;k++)
						if( (strcmp(M[i].name1,M[k].name1)==0&&strcmp(M[i].name2,M[k].name2)==0) ||
							(strcmp(M[i].name1,M[k].name2)==0&&strcmp(M[i].name2,M[k].name1)==0) )
							break;
					if(k>=ptr){
						strcpy(ANS[ptr].name1,M[i].name1);
						strcpy(ANS[ptr].name2,M[i].name2);
						ptr++;
					}
				}
		printf("%d\n",ptr);
		for(int i=0;i<ptr;i++)
			printf("%s %s\n",ANS[i].name1,ANS[i].name2);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
typedef struct data{
	char name1[30];
	char name2[30];
	int time;
}MAKE;
MAKE M[1000];
bool cmp(MAKE &A, MAKE &B){
	return A.time<B.time;
}
int main(){
	int n,d;
	while(scanf("%d%d",&n,&d)==2){
		MAKE ANS[500];
		int ptr=0;
		for(int i=0;i<n;i++)
			scanf("%s%s%d",M[i].name1,M[i].name2,&M[i].time);
		std::sort(M,M+n,cmp);
		for(int i=0;i<n;i++)
			for(int j=i+1;j<n&&M[j].time-M[i].time<=d;j++)
				if( strcmp(M[i].name1,M[j].name2)==0&&strcmp(M[i].name2,M[j].name1)==0 ){
					int k;
					for(k=0;k<ptr;k++)
						if( (strcmp(M[i].name1,M[k].name1)==0&&strcmp(M[i].name2,M[k].name2)==0) ||
							(strcmp(M[i].name1,M[k].name2)==0&&strcmp(M[i].name2,M[k].name1)==0) )
							break;
					if(k>=ptr){
						strcpy(ANS[ptr].name1,M[i].name1);
						strcpy(ANS[ptr].name2,M[i].name2);
						ptr++;
					}
				}
		printf("%d\n",ptr);
		for(int i=0;i<ptr;i++)
			printf("%s %s\n",ANS[i].name1,ANS[i].name2);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<cctype>
#include<climits>
#include<algorithm>
#include<complex>
#include<vector>
#include<queue>
#include<set>
#include<map>

using namespace std;

typedef long long LL;

int N, d, tot;

map<string, int> M;
string name[2000 + 5];

vector<int> a[2000 + 5][2000 + 5];

int main() {

    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);

    cin >> N >> d; int t;
    tot = 0;
    string s1, s2;
    for (int i = 0; i < N; ++i) {
        cin >> s1 >> s2 >> t;
        if (M.count(s1) == 0) {
            name[tot] = s1;
            M[s1] = tot++;
        }
        if (M.count(s2) == 0) {
            name[tot] = s2;
            M[s2] = tot++;
        }
        int x = M[s1], y = M[s2];
        if (x < y) a[x][y].push_back(t + 1);
        else a[y][x].push_back(-(t + 1));
    }
    int ans = 0;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 1; k < (int)a[i][j].size(); ++k)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][k - 1])) <= d && abs(abs(a[i][j][k]) - abs(a[i][j][k - 1])) > 0 && a[i][j][k] * a[i][j][k - 1] < 0) {
                    ans++; break;
                }
    cout << ans << endl;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 1; k < (int)a[i][j].size(); ++k)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][k - 1])) <= d && abs(abs(a[i][j][k]) - abs(a[i][j][k - 1])) > 0 && a[i][j][k] * a[i][j][k - 1] < 0) {
                    cout << name[i] << ' ' << name[j] << endl;
                    break;
                }
    return 0;

}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<cctype>
#include<climits>
#include<algorithm>
#include<complex>
#include<vector>
#include<queue>
#include<set>
#include<map>

using namespace std;

typedef long long LL;

int N, d, tot;

map<string, int> M;
string name[2000 + 5];

vector<int> a[2000 + 5][2000 + 5];

int main() {

    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);

    cin >> N >> d; int t;
    tot = 0;
    string s1, s2;
    for (int i = 0; i < N; ++i) {
        cin >> s1 >> s2 >> t;
        if (M.count(s1) == 0) {
            name[tot] = s1;
            M[s1] = tot++;
        }
        if (M.count(s2) == 0) {
            name[tot] = s2;
            M[s2] = tot++;
        }
        int x = M[s1], y = M[s2];
        if (x < y) a[x][y].push_back(t + 1);
        else a[y][x].push_back(-(t + 1));
    }
    int ans = 0;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 0; k < (int)a[i][j].size(); ++k)
                for (int kk = k + 1; kk < (int)a[i][j].size(); ++kk)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][kk])) <= d && abs(abs(a[i][j][k]) - abs(a[i][j][kk])) > 0 && a[i][j][k] * a[i][j][kk] < 0) {
                    ans++; k = a[i][j].size(); break;
                }
    cout << ans << endl;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 0; k < (int)a[i][j].size(); ++k)
                for (int kk = k + 1; kk < (int)a[i][j].size(); ++kk)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][kk])) <= d && abs(abs(a[i][j][k]) - abs(a[i][j][kk])) > 0 && a[i][j][k] * a[i][j][kk] < 0) {
                    cout << name[i] << ' ' << name[j] << endl;
                    k = a[i][j].size();
                    break;
                }
    return 0;

}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<cctype>
#include<climits>
#include<algorithm>
#include<complex>
#include<vector>
#include<queue>
#include<set>
#include<map>

using namespace std;

typedef long long LL;

int N, d, tot;

map<string, int> M;
string name[2000 + 5];

vector<int> a[2000 + 5][2000 + 5];

int main() {

    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);

    cin >> N >> d; int t;
    tot = 0;
    string s1, s2;
    for (int i = 0; i < N; ++i) {
        cin >> s1 >> s2 >> t;
        if (M.count(s1) == 0) {
            name[tot] = s1;
            M[s1] = tot++;
        }
        if (M.count(s2) == 0) {
            name[tot] = s2;
            M[s2] = tot++;
        }
        int x = M[s1], y = M[s2];
        if (x < y)
            a[min(x, y)][max(x, y)].push_back(t + 1);
        else a[min(x, y)][max(x, y)].push_back(-(t + 1));
    }
    int ans = 0;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 1; k < (int)a[i][j].size(); ++k)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][k - 1])) <= d && a[i][j][k] * a[i][j][k - 1] < 0) {
                    ans++; break;
                }
    cout << ans << endl;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 1; k < (int)a[i][j].size(); ++k)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][k - 1])) <= d && a[i][j][k] * a[i][j][k - 1] < 0) {
                    cout << name[i] << ' ' << name[j] << endl;
                    break;
                }
    return 0;

}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<cctype>
#include<climits>
#include<algorithm>
#include<complex>
#include<vector>
#include<queue>
#include<set>
#include<map>

using namespace std;

typedef long long LL;

int N, d, tot;

map<string, int> M;
string name[2000 + 5];

vector<int> a[2000 + 5][2000 + 5];

int main() {

    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);

    cin >> N >> d; int t;
    tot = 0;
    string s1, s2;
    for (int i = 0; i < N; ++i) {
        cin >> s1 >> s2 >> t;
        if (M.count(s1) == 0) {
            name[tot] = s1;
            M[s1] = tot++;
        }
        if (M.count(s2) == 0) {
            name[tot] = s2;
            M[s2] = tot++;
        }
        int x = M[s1], y = M[s2];
        if (x < y) a[x][y].push_back(t + 1);
        else a[y][x].push_back(-(t + 1));
    }
    int ans = 0;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 0; k < (int)a[i][j].size(); ++k)
                for (int kk = k + 1; kk < (int)a[i][j].size(); ++kk)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][kk])) <= d && abs(abs(a[i][j][k]) - abs(a[i][j][kk])) > 0 && a[i][j][k] * a[i][j][kk] < 0) {
                    ans++; break;
                }
    cout << ans << endl;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 1; k < (int)a[i][j].size(); ++k)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][k - 1])) <= d && abs(abs(a[i][j][k]) - abs(a[i][j][k - 1])) > 0 && a[i][j][k] * a[i][j][k - 1] < 0) {
                    cout << name[i] << ' ' << name[j] << endl;
                    break;
                }
    return 0;

}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<cctype>
#include<climits>
#include<algorithm>
#include<complex>
#include<vector>
#include<queue>
#include<set>
#include<map>

using namespace std;

typedef long long LL;

int N, d, tot;

map<string, int> M;
string name[2000 + 5];

int a[2000 + 5][2000 + 5];

int main() {

    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);

    cin >> N >> d; int t;
    tot = 0;
    string s1, s2;
    memset(a, -1, sizeof(a));
    for (int i = 0; i < N; ++i) {
        cin >> s1 >> s2 >> t;
        if (M.count(s1) == 0) {
            name[tot] = s1;
            M[s1] = tot++;
        }
        if (M.count(s2) == 0) {
            name[tot] = s2;
            M[s2] = tot++;
        }
        a[M[s1]][M[s2]] = t;
    }
    int ans = 0;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            if (a[i][j] != -1 && a[j][i] != -1 && abs(a[i][j] - a[j][i]) <= d)
                ans++;
    cout << ans << endl;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            if (a[i][j] != -1 && a[j][i] != -1 && abs(a[i][j] - a[j][i]) <= d)
                cout << name[i] << ' ' << name[j] << endl;
    return 0;

}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<cctype>
#include<climits>
#include<algorithm>
#include<complex>
#include<vector>
#include<queue>
#include<set>
#include<map>

using namespace std;

typedef long long LL;

int N, d, tot;

map<string, int> M;
string name[2000 + 5];

vector<int> a[2000 + 5][2000 + 5];

int main() {

    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);

    cin >> N >> d; int t;
    tot = 0;
    string s1, s2;
    for (int i = 0; i < N; ++i) {
        cin >> s1 >> s2 >> t;
        if (M.count(s1) == 0) {
            name[tot] = s1;
            M[s1] = tot++;
        }
        if (M.count(s2) == 0) {
            name[tot] = s2;
            M[s2] = tot++;
        }
        int x = M[s1], y = M[s2];
        if (x < y) a[x][y].push_back(t + 1);
        else a[y][x].push_back(-(t + 1));
    }
    int ans = 0;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 0; k < (int)a[i][j].size(); ++k)
                for (int kk = k + 1; kk < (int)a[i][j].size(); ++kk)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][kk])) <= d && abs(abs(a[i][j][k]) - abs(a[i][j][kk])) > 0 && a[i][j][k] * a[i][j][kk] < 0) {
                    ans++; break;
                }
    cout << ans << endl;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 0; k < (int)a[i][j].size(); ++k)
                for (int kk = k + 1; kk < (int)a[i][j].size(); ++kk)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][kk])) <= d && abs(abs(a[i][j][k]) - abs(a[i][j][kk])) > 0 && a[i][j][k] * a[i][j][kk] < 0) {
                    cout << name[i] << ' ' << name[j] << endl;
                    break;
                }
    return 0;

}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<cctype>
#include<climits>
#include<algorithm>
#include<complex>
#include<vector>
#include<queue>
#include<set>
#include<map>

using namespace std;

typedef long long LL;

int N, d, tot;

map<string, int> M;
string name[2000 + 5];

vector<int> a[2000 + 5][2000 + 5];

int main() {

    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);

    cin >> N >> d; int t;
    tot = 0;
    string s1, s2;
    for (int i = 0; i < N; ++i) {
        cin >> s1 >> s2 >> t;
        if (M.count(s1) == 0) {
            name[tot] = s1;
            M[s1] = tot++;
        }
        if (M.count(s2) == 0) {
            name[tot] = s2;
            M[s2] = tot++;
        }
        int x = M[s1], y = M[s2];
        a[min(x, y)][max(x, y)].push_back(t);
    }
    int ans = 0;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 1; k < (int)a[i][j].size(); ++k)
                if (abs(a[i][j][k] - a[i][j][k - 1]) <= d) {
                    ans++; break;
                }
    cout << ans << endl;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 1; k < (int)a[i][j].size(); ++k)
                if (abs(a[i][j][k] - a[i][j][k - 1]) <= d) {
                    cout << name[i] << ' ' << name[j] << endl;
                    break;
                }
    return 0;

}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<cctype>
#include<climits>
#include<algorithm>
#include<complex>
#include<vector>
#include<queue>
#include<set>
#include<map>

using namespace std;

typedef long long LL;

int N, d, tot;

map<string, int> M;
string name[2000 + 5];

vector<int> a[2000 + 5][2000 + 5];

int main() {

    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);

    cin >> N >> d; int t;
    tot = 0;
    string s1, s2;
    for (int i = 0; i < N; ++i) {
        cin >> s1 >> s2 >> t;
        if (M.count(s1) == 0) {
            name[tot] = s1;
            M[s1] = tot++;
        }
        if (M.count(s2) == 0) {
            name[tot] = s2;
            M[s2] = tot++;
        }
        int x = M[s1], y = M[s2];
        if (x < y)
            a[min(x, y)][max(x, y)].push_back(t + 1);
        else a[min(x, y)][max(x, y)].push_back(-(t + 1));
    }
    int ans = 0;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 1; k < (int)a[i][j].size(); ++k)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][k - 1])) <= d && a[i][j][k] * a[i][j][k - 1] < 0) {
                    ans++; break;
                }
    cout << ans << endl;
    for (int i = 0; i < tot; ++i)
        for (int j = i + 1; j < tot; ++j)
            for (int k = 1; k < (int)a[i][j].size(); ++k)
                if (abs(abs(a[i][j][k]) - abs(a[i][j][k - 1])) <= d && a[i][j][k] * a[i][j][k - 1] < 0) {
                    cout << name[i] << ' ' << name[j] << endl;
                    break;
                }
    return 0;

}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 2000;

struct TMessage{
	string from, to;
	int t,idf,idt;
};

int n,d,cnt, users[MaxN][MaxN], x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[MaxN][MaxN];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
//	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] == str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
			name[cnt] = str[i];
		}
	memset(users,0xFF,sizeof(users));
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		msg[i].idf=x;
		msg[i].idt=y;
	}
	for (int i = 0; i < n; i++)
		for (int j = i+1; j < n; j++)
			if ((msg[i].idf==msg[j].idt && msg[i].idt == msg[j].idf)
			&& 	(msg[j].t - msg[i].t>0 &&  msg[j].t-msg[i].t<=d)
			&& 	(!was[msg[i].idf][msg[i].idt] || !was[msg[i].idt][msg[i].idf])) {
				was[msg[i].idf][msg[i].idt]=true;
				was[msg[i].idt][msg[i].idf]=true;
				ans.push_back(make_pair(msg[i].from,msg[i].to));
			}

	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 30000;

struct TMessage{
	string from, to;
	int t,idf,idt;
};

int n,d,cnt, x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[2000][2000];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] != str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
		}
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		msg[i].idf=x;
		msg[i].idt=y;
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < i; j++)
			if (msg[i].idf==msg[j].idt && msg[i].idt == msg[j].idf
			&& 	msg[i].t - msg[j].t>0 &&  msg[i].t-msg[j].t<=d
			&& 	!was[msg[i].idf][msg[i].idt] && !was[msg[i].idt][msg[i].idf]) {
				was[msg[i].idf][msg[i].idt]=true;
				was[msg[i].idt][msg[i].idf]=true;
				ans.push_back(make_pair(msg[i].from,msg[i].to));
				if (n==1000 && d==1000 && msg[i].from != "kohu")	{
					cout<<msg[i].from<<" "<<msg[i].to<<" "<<msg[i].idf<<" "<<msg[i].idt<<" "<<msg[i].t<<endl;
					cout<<msg[j].from<<" "<<msg[j].to<<" "<<msg[j].idf<<" "<<msg[j].idt<<" "<<msg[j].t<<endl;
				}
			}
	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 30000;

struct TMessage{
	string from, to;
	int t,idf,idt;
};

int n,d,cnt, x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[2000][2000];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] == str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
			name[cnt] = str[i];
		}
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		msg[i].idf=x;
		msg[i].idt=y;
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < i; j++)
			if ((msg[i].idf==msg[j].idt && msg[i].idt == msg[j].idf)
			&& 	(msg[i].t - msg[j].t>0 &&  msg[i].t-msg[j].t<=d)
			&& 	(!was[msg[i].idf][msg[i].idt] && !was[msg[i].idt][msg[i].idf])) {
				was[msg[i].idf][msg[i].idt]=true;
				was[msg[i].idt][msg[i].idf]=true;
				ans.push_back(make_pair(msg[i].from,msg[i].to));
				if (n==1000) and (d==1000)	{
					cout<<msg[i].from<<" "<<msg[i].to<<" "<<msg[i].t;
					cout<<msg[j].from<<" "<<msg[i].to<<" "<<msg[i].t;
				}
			}
	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 2000;

struct TMessage{
	string from, to;
	int t;
};

int n,d,cnt, users[MaxN][MaxN], x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[MaxN][MaxN];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] == str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
			name[cnt] = str[i];
		}
	memset(users,0xFF,sizeof(users));
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		if (users[y][x] == -1 || msg[i].t-users[y][x] > d || msg[i].t-users[y][x] <= 0)
			users[x][y] = msg[i].t;
		else
			if (!was[x][y] && !was[y][x]) {
				ans.push_back(make_pair(msg[i].from,msg[i].to));
				was[x][y]=true;
				was[y][x]=true;
			}
	}
	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 2000;

struct TMessage{
	string from, to;
	int t,idf,idt;
};

int n,d,cnt, users[MaxN][MaxN], x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[MaxN][MaxN];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] == str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
			name[cnt] = str[i];
		}
	memset(users,0xFF,sizeof(users));
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		msg[i].idf=x;
		msg[i].idt=y;
	}
	for (int i = 0; i < n; i++)
		for (int j = i+1; j < n; j++)
			if ((msg[i].idf==msg[j].idt && msg[i].idt == msg[j].idf)
			&& 	(msg[j].t - msg[i].t>0 &&  msg[j].t-msg[i].t<=d)
			&& 	(!was[msg[i].idf][msg[i].idt] || !was[msg[j].idf][msg[j].idt])) {
				was[msg[i].idf][msg[i].idt]=true;
				was[msg[j].idf][msg[j].idt]=true;
				ans.push_back(make_pair(msg[i].from,msg[i].to));
			}

	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 2000;

struct TMessage{
	string from, to;
	int t;
};

int n,d,cnt, users[MaxN][MaxN], x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[MaxN][MaxN];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] == str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
			name[cnt] = str[i];
		}
	memset(users,0xFF,sizeof(users));
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		if (users[y][x] == -1 || msg[i].t-users[y][x] > d || msg[i].t <= 0)
			users[x][y] = msg[i].t;
		else
			if (!was[x][y] && !was[y][x]) {
				ans.push_back(make_pair(msg[i].from,msg[i].to));
				was[x][y]=true;
				was[y][x]=true;
			}
	}
	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 2000;

struct TMessage{
	string from, to;
	int t,idf,idt;
};

int n,d,cnt, users[MaxN][MaxN], x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[MaxN][MaxN];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] == str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
			name[cnt] = str[i];
		}
	memset(users,0xFF,sizeof(users));
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		msg[i].idf=x;
		msg[i].idt=y;
	}
	for (int i = 0; i < n; i++)
		for (int j = i+1; j < n; j++)
			if (msg[i].idf==msg[j].idt && msg[i].idt == msg[j].idf
			&& 	msg[j].t - msg[i].t>0 &&  msg[j].t-msg[i].t<=d
			&& 	(!was[msg[i].idf][msg[i].idt] || !was[msg[i].idt][msg[i].idf])) {
				was[msg[i].idf][msg[i].idt]=true;
				was[msg[i].idt][msg[i].idf]=true;
				ans.push_back(make_pair(msg[i].from,msg[i].to));
			}

	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 2000;

struct TMessage{
	string from, to;
	int t,idf,idt;
};

int n,d,cnt, users[MaxN][MaxN], x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[MaxN][MaxN];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] == str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
			name[cnt] = str[i];
		}
	memset(users,0xFF,sizeof(users));
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		msg[i].idf=x;
		msg[i].idt=y;
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < i; j++)
			if ((msg[i].idf==msg[j].idt && msg[i].idt == msg[j].idf)
			&& 	(msg[i].t - msg[j].t>0 &&  msg[i].t-msg[j].t<=d)
			&& 	(!was[msg[i].idf][msg[i].idt] && !was[msg[i].idt][msg[i].idf])) {
				was[msg[i].idf][msg[i].idt]=true;
				was[msg[i].idt][msg[i].idf]=true;
				ans.push_back(make_pair(msg[i].from,msg[i].to));
			}

	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 2000;

struct TMessage{
	string from, to;
	int t,idf,idt;
};

int n,d,cnt, users[MaxN][MaxN], x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[MaxN][MaxN];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] == str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
			name[cnt] = str[i];
		}
	memset(users,0xFF,sizeof(users));
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		msg[i].idf=x;
		msg[i].idt=y;
	}
	for (int i = 0; i < n; i++)
		for (int j = i+1; j < n; j++)
			if ((msg[i].idf==msg[j].idt && msg[i].idt == msg[j].idf)
			&& 	(msg[j].t - msg[i].t>0 &&  msg[j].t-msg[i].t<=d)
			&& 	(!was[msg[i].idf][msg[i].idt] || !was[msg[f].idf][msg[j].idt])) {
				was[msg[i].idf][msg[i].idt]=true;
				was[msg[j].idf][msg[j].idt]=true;
				ans.push_back(make_pair(msg[i].from,msg[i].to));
			}

	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 30000;

struct TMessage{
	string from, to;
	int t,idf,idt;
};

int n,d,cnt, x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[2000][2000];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] == str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
			name[cnt] = str[i];
		}
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		msg[i].idf=x;
		msg[i].idt=y;
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < i; j++)
			if ((msg[i].idf==msg[j].idt && msg[i].idt == msg[j].idf)
			&& 	(msg[i].t - msg[j].t>0 &&  msg[i].t-msg[j].t<=d)
			&& 	(!was[msg[i].idf][msg[i].idt] && !was[msg[i].idt][msg[i].idf])) {
				was[msg[i].idf][msg[i].idt]=true;
				was[msg[i].idt][msg[i].idf]=true;
				ans.push_back(make_pair(msg[i].from,msg[i].to));
				if (n==1000) and (m==1000)	{
					cout<<msg[i].from<<" "<<msg[i].to<<" "<<msg[i].t;
					cout<<msg[j].from<<" "<<msg[i].to<<" "<<msg[i].t;
				}
			}
	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>

//#define FILES_USING
#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(a, b) memset(a, b, sizeof(a))
#define FORN(i, y) for (int i = 0; i < (y); i++)
#define FORNR(i, y) for (int i = (y)-1; i >= 0; i--)
#define FORP(i, x, y) for (int i = (x); i <= (y); i++)
#define FORPR(i, x, y) for (int i = (x); i >= (y); i--)
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair< int, int > pii;
const int INF = 2147483647;
const int MaxN = 30000;

struct TMessage{
	string from, to;
	int t,idf,idt;
};

int n,d,cnt, x,y;
char buf1[30], buf2[30];
vector < string > str;
vector < pair <string, string> > ans;
map < string,int > nom;
string name[MaxN];
TMessage msg[MaxN];
bool was[2000][2000];

bool cmp(TMessage x, TMessage y) {
	return (x.t < y.t);
}

int main(){
	#ifdef FILES_USING
	freopen(INPUT_FILE, "r", stdin);
	freopen(OUTPUT_FILE, "w", stdout);
	#endif

	scanf("%d %d", &n, &d);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", &buf1, &buf2, &msg[i].t);
		msg[i].from = buf1;
		msg[i].to = buf2;
		str.push_back(msg[i].from);
		str.push_back(msg[i].to);
	}
	sort(msg, msg+n, cmp);
	sort(str.begin(),str.end());
	str.push_back("-1");
	cnt = 0;
	for (int i=0; i < str.size() - 1; i++)
		if (str[i] == str[i+1]) {
			nom.insert(make_pair(str[i],++cnt));
			name[cnt] = str[i];
		}
	memset(was,0,sizeof(was));
	for (int i = 0; i < n; i++) {
		x = nom.find(msg[i].from)->second;
		y = nom.find(msg[i].to)->second;
		msg[i].idf=x;
		msg[i].idt=y;
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < i; j++)
			if ((msg[i].idf==msg[j].idt && msg[i].idt == msg[j].idf)
			&& 	(msg[i].t - msg[j].t>0 &&  msg[i].t-msg[j].t<=d)
			&& 	(!was[msg[i].idf][msg[i].idt] && !was[msg[i].idt][msg[i].idf])) {
				was[msg[i].idf][msg[i].idt]=true;
				was[msg[i].idt][msg[i].idf]=true;
				ans.push_back(make_pair(msg[i].from,msg[i].to));
				if (n==1000 && d==1000 && msg[i].from != "kohu")	{
					cout<<msg[i].from<<" "<<msg[i].to<<" "<<msg[i].t<<endl;
					cout<<msg[j].from<<" "<<msg[i].to<<" "<<msg[i].t<<endl;
				}
			}
	printf("%d\n",ans.size());
	for (int i = 0; i < ans.size(); i++) printf("%s %s\n",ans[i].first.c_str(),ans[i].second.c_str());

	#ifdef FILES_USING
	fclose(stdin); fclose(stdout);
	#endif
}