#!/usr/bin/env python3

n,k,c = map(int, input().split())

contacts = [[] for __ in range(k + 1)]
last_day = [-1 for i in range(n + 1)]
for i in range(c):
    a,b,c = map(int, input().split())
    contacts[c].append((a,b))
    contacts[c].append((b,a))
    last_day[a] = max(last_day[a], c)
    last_day[b] = max(last_day[b], c)


previous = set(i for i in range(1, n + 1) if last_day[i] <= 1)

for day in range(1, k + 1):
    current = set()
    for a, b in contacts[day]:
        if day >= last_day[a] - 1 and b in previous:
            current.add(a)
    previous = current

previous = list(sorted(previous))
print(len(previous))
for x in previous:
    print(x)
