#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
const int maxn = 2200;
map<string,int> mp;
map<int,string> mp1;
map<string,int> gang;//map根据第一个键值排序
int n,k;
int weight[maxn],G[maxn][maxn] = {0};
int num = 0;
bool vis[maxn] = {false};
int findi(string a)
{
if(mp.find(a) == mp.end())
{
mp[a] = num;
mp1[num++] = a;
}
return mp[a];
}
void dfs(int v,int &ber,int &head,int &total)
{
ber++;
vis[v] = true;
if(weight[v] > weight[head])
head = v;
for(int i = 0; i < num; i++)
{
if(G[v][i] > 0)
{
total += G[v][i];
G[v][i] = G[i][v] = 0;
if(vis[i] == false)
dfs(i,ber,head,total);
}
}
}
int main()
{
int u,v,t;
string s;
scanf("%d %d",&n,&k);
for(int i = 0; i < n; i++)
{
cin>>s; u = findi(s);
cin>>s; v = findi(s);
scanf("%d",&t);
G[u][v] +=t; G[v][u] += t;
weight[u]+=t;weight[v]+=t;
}
for(int i = 0; i < num; i++)
{
if(vis[i] == false)
{
int ber = 0,head = i,total = 0;
dfs(i,ber,head,total);
if(ber > 2 && total > k)
{
gang[mp1[head]] = ber;
}
}
}
printf("%d\n",gang.size());
for(map<string,int>::iterator it = gang.begin(); it != gang.end(); it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}