#include<string>
#include<iostream>
#include<unordered_map>
#include<vector>
#include<algorithm>
using namespace std;
struct node {
int value;
int id;
};
int n, k, numb = 0,numbgang=0;
vector<int> score(2000, 0);
int all[2000][2000] = { 0 };
vector<bool> visited(2000, false);
vector<node> head;
unordered_map<int, string> id2n;
unordered_map<string, int> n2id;
bool cmp(node& a, node& b) {
return id2n[a.id] < id2n[b.id];
}
void DFS(int root, int& totalsocore)
{
visited[root] = true;
if (score[root] > score[head[numbgang - 1].id]) {
head[numbgang - 1].id = root;
}
for (int i = 0; i < numb; i++) {
if (all[root][i]!=0&&visited[i] == false) {
head[numbgang - 1].value++;
totalsocore += score[i];
DFS(i, totalsocore);
}
}
}
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
string a, b;
int time;
cin >> a >> b >> time;
if (!n2id.count(a)) {
n2id[a] = numb;
id2n[numb++] = a;
}
if (!n2id.count(b)) {
n2id[b] = numb;
id2n[numb++] = b;
}
score[n2id[a]] += time;
score[n2id[b]] += time;
all[n2id[a]][n2id[b]]+=time;
all[n2id[b]][n2id[a]]+=time;
}
for (int i = 0; i < numb; i++) {
if (visited[i] == false) {
numbgang++;
int totalsocore = score[i];
head.push_back(node{ 1,i});
DFS(i,totalsocore);
if ((totalsocore/2) <= k||head[numbgang-1].value<=2) {
numbgang--;
head.erase(head.end()- 1);
}
}
}
sort(head.begin(), head.end(), cmp);
cout << numbgang;
for (int i = 0; i < numbgang; i++) {
cout << "\n" << id2n[head[i].id] << " " << head[i].value;
}
return 0;
}