#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int c;
struct node{
string id, name;
int grade;
};
bool cmp(node &a, node &b){
if(c == 1){
return a.id < b.id;
}else if(c == 2){
if(a.name == b.name) return a.id < b.id;
return a.name < b.name;
}else{
if(a.grade == b.grade) return a.id < b.id;
return a.grade < b.grade;
}
}
int main(){
int n;
cin >> n >> c;
vector<node> v(n);
for(int i = 0; i < n; i++){
cin >> v[i].id >> v[i].name >> v[i].grade;
}
sort(v.begin(), v.end(), cmp);
for(int i = 0; i < n; i++){
printf("%s %s %d\n", v[i].id.c_str(), v[i].name.c_str(), v[i].grade);
}
return 0;
}