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

int n, k;


map<int, vector<pair<int, int> >  > mp;  //u,p
map<int, vector<int> > IDS;

priority_queue<pair<int, pair<int,int> > > q;  //selected, penalty university

map<int, int> sel;

vector<int> ans;

map<int, deque<pair<int, int> > > team;  //university, pena,id
int main(){
	cin >> n >> k;
	for (int i = 0; i < n; i++){
		int s, p, u;
		scanf("%d%d%d", &s, &p, &u);
		mp[-s].push_back(make_pair(u, p));
		IDS[-s].push_back(i);
	}
	for (auto it = mp.begin(); it != mp.end(); it++){
		vector<pair<int, int> > &v = (*it).second;
		for (int i = 0; i < v.size(); i++){
			team[v[i].first].push_back(make_pair(v[i].second, IDS[(*it).first][i]));
		}
		for (auto it = team.begin(); it != team.end(); it++){
			sort((*it).second.begin(), (*it).second.end());
			q.push(make_pair(-sel[(*it).first], make_pair(-(*it).second[0].first, (*it).first)));
		}
		while (k>0 && q.size()){
			pair<int, pair<int, int> > b = q.top();
			q.pop();
			k--;
			int uni = b.second.second;
			sel[uni]++;
			ans.push_back(team[uni].front().second);
			team[uni].pop_front();
			if (team[uni].size()){
				q.push(make_pair(-sel[uni], make_pair(-team[uni].front().first, uni)) );
			}
		}
		team.clear();
	}
	for (int i = 0; i < ans.size(); i++){
		printf("%d\n", ans[i]);
	}
	return 0;
}