import heapq from collections import defaultdict n, k = map(int, input().split()) teams = [] for idx in range(n): s, p, u = map(int, input().split()) teams.append((s, p, u, idx)) teams_by_u = defaultdict(list) for s, p, u, idx in teams: teams_by_u[u].append((s, p, idx)) for u in teams_by_u: teams_by_u[u].sort(key=lambda x: (-x[0], x[1])) current_idx = defaultdict(int) count = defaultdict(int) heap = [] for u in teams_by_u: if teams_by_u[u]: s, p, idx = teams_by_u[u][0] heapq.heappush(heap, (-s, count[u], p, u, idx)) current_idx[u] = 0 selected = [] for _ in range(k): s_neg, cnt_u, p, u, idx = heapq.heappop(heap) selected.append(idx) count[u] += 1 current = current_idx[u] if current + 1 < len(teams_by_u[u]): current_idx[u] += 1 next_s, next_p, next_idx = teams_by_u[u][current_idx[u]] heapq.heappush(heap, (-next_s, count[u], next_p, u, next_idx)) for team_idx in selected: print(team_idx)